0xCord provides a RESTful API on top of Chainlink VRF v2, making it easy for developers to generate verifiably random numbers for their applications. If you're building a game in Unity and need a random number generator, integrating 0xCord's API can be a great solution. In this blog post, we'll go through the steps you need to take to integrate 0xCord's API into your Unity game.
Follow along on Github here: https://github.com/Made-For-Gamers/game-jam-webgl-template
(1) Get an API key and URL
To use 0xCord's API for Chainlink VRF v2, you'll need an 0xCord API key. You can get these by signing up on the 0xCord website. Once you have your API key and URL, you can use them to send requests to the API.
a. Sign up for an account on 0xCord
To use the 0xCord VRF API, you need to sign up for an account on their website. Go to https://0xcord.com and follow the sign-up process.
b. Create a new Dapp
Once you've signed up, create a new Dapp by following these steps:
- Click on "Create Dapp" on the 0xCord dashboard.
- Enter a name for your Dapp and click on "Create".
c. Provision Chainlink for your Dapp
After you've created your Dapp, you need to provision Chainlink for it by following these steps:
- Click on “Add new service”.
- Select “Chainlink VRF V2” from the Infra hub and select the dapp name from the dropdown.
Once you've provisioned Chainlink for your Dapp, you can get your API key.
d. Get your API key
To get your API key, you can use the "Fetch Config Vars" button on the dapp page.
- Copy the "API KEY" value.
Your API key is now available to use with the 0xCord VRF API.
(2) Create a script in Unity
Next, create a script in Unity that will send requests to the 0xcord VRF API. The example code we provided above shows how to do this. You can use the UnityWebRequest class to send HTTP requests to the API.
(3) Call the API
To generate a random number using the 0xcord VRF API, you'll need to call the API with your API key and URL. In the example code, the URL is set to "https://0xcord.com/api/vrfv2/requestRandomNumber?network=fuji&numWords=1", which requests a single random number from the "fuji" network.
The authToken variable in the code is used to set the API key. You should replace this value with your own API key.
[SerializeField] private TextMeshProUGUI txtHeading; //text field to display result
[SerializeField] private Button BtnCallApi; //Button that acitons the call
[SerializeField] private string apiKey; // Add your key in the inspector
//Button calls this method
public void CallVRF()
{
StartCoroutine(RequestRandomNumber());
}
private IEnumerator RequestRandomNumber()
{
BtnCallApi.enabled = false;
txtHeading.text = "Requesting random number from VRF. Please wait...";
string url = "https://0xcord.com/api/vrfv2/requestRandomNumber?network=fuji&numWords=1";
string authToken = apiKey;
UnityWebRequest request = UnityWebRequest.Post(url, "");
request.SetRequestHeader("Authorization", authToken);
yield return request.SendWebRequest();
// ....
}
An example can be found here: https://github.com/Made-For-Gamers/game-jam-webgl-template/blob/f0ad0f224d8bd0f48868aab0f0d77b3f22a43b5b/Assets/Scripts/0xcord/Chainlink_API.cs
(4) Parse the response
Once you've sent the request to the API, you'll need to parse the response. In the example code, the response is returned as a JSON string. You can use the JsonUtility class to parse the JSON into a C# object.
Create another script class for the returned JSON structure:
using System;
[Serializable]
public class RandomNumberResponse
{
public Data data;
[Serializable]
public class Data
{
public bool success;
public string requestId;
public string transactionHash;
public string url;
public string[] randomNumber;
}
}
An example can be found here: https://github.com/Made-For-Gamers/game-jam-webgl-template/blob/f0ad0f224d8bd0f48868aab0f0d77b3f22a43b5b/Assets/Scripts/0xcord/Return Data/RandomNumberResponse.cs
Add the following inside the RequestRandomNumber method:
//...
if (request.result == UnityWebRequest.Result.Success)
{
string json = request.downloadHandler.text;
RandomNumberResponse response = JsonConvert.DeserializeObject<RandomNumberResponse>(json);
if (response != null && response != null && response.data.randomNumber != null && response.data.randomNumber.Length > 0)
{
string randomNumber = response.data.randomNumber[0];
txtHeading.text = "Random number: " + randomNumber;
Debug.Log("requestId: " + response.data.requestId);
Debug.Log("transactionHash: " + response.data.transactionHash);
Debug.Log("url: " + response.data.url);
Debug.Log("randomNumber: " + randomNumber);
}
else
{
Debug.Log("Failed to parse response");
}
}
else
{
Debug.Log("Error: " + request.error);
}
BtnCallApi.enabled = true;
(5) Use the random number in your game
Finally, you can use the random number generated by the 0xCord VRF API in your game. In the example code, the random number is displayed in the Unity UI, using the ChangeText method. You can use the random number in any way you need to in your game. You can also use the transaction hash and URL, and show this information to your players.
In conclusion, integrating 0xCord's VRF API into your Unity game is a straightforward process. By following the steps outlined above, you can easily generate verifiably random numbers for your game. The 0xCord API is built on top of the Chainlink VRF infrastructure, which ensures the randomness of the generated numbers, making it a reliable solution for various use cases, including gaming, gambling, and security applications.
Thanks!