-
Notifications
You must be signed in to change notification settings - Fork 3
API Requests (GET, POST)
Elrond Network provides free to use API to interact wiht the blockchain. The API is built to allow you to create apps or integrations quickly and easily.
The official APIs and docs can be found here:
https://api.elrond.com/
https://gateway-docs.elrond.com/
Note: If you are using devnet or testnet to develop and debug your app use the devnet or testnet APIs accordingly.
Get requests are used to receive information from the blockchain.
/// <summary>
/// Generic API Get request
/// </summary>
/// <typeparam name="T">Return type</typeparam>
/// <param name="url">Get API url</param>
/// <param name="CompleteMethod">Complete listener (operation status, error message, return data</param>
public static void GetRequest<T>(string url, UnityAction<OperationStatus, string, T> CompleteMethod)
Here is a simple example used to get the info about an erd address.
//construct the url for the Get request
string url = "https://devnet-api.elrond.com/accounts/erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf";
//make the Get request
Manager.GetRequest<AccountDto>(url, CompleteMethodGet);
//receive the result inside Complete Method
private void CompleteMethodGet(OperationStatus operationStatus, string message, AccountDto result)
{
if (operationStatus == OperationStatus.Complete)
{
Debug.Log(result.Address);
}
else
{
Debug.LogError(message + " " + result);
}
}
Post requests are used to send information to the blockchain and receive a response.
/// <summary>
/// Make a POST request to MultiversX APIs
/// </summary>
/// <param name="url">Post url</param>
/// <param name="jsonData">json data to send</param>
/// <param name="CompleteMethod">Complete listener (operation status, error message, return data)</param>
public static void PostRequest<T>(string url, string jsonData, UnityAction<OperationStatus, string, T> CompleteMethod)
Here is a simple example on how to get the gas cost for a transaction before submitting it to the blockchain:
//construct the url
string url = "https://devnet-gateway.elrond.com/transaction/cost";
//construct the params as a json string
string json = "{" +
"\"nonce\":0," +
"\"sender\":\"erd1lgp3ezf2wfkejnu0sm5y9g4x3ad05gr8lfc0g69vvdwwj0wjv0gscv2w4s\"," +
"\"receiver\":\"erd1jza9qqw0l24svfmm2u8wj24gdf84hksd5xrctk0s0a36leyqptgs5whlhf\"," +
"\"value\":\"1000000000000000\"," +
"\"gasPrice\":1000000000," +
"\"gasLimit\":89000," +
"\"data\":\"WW91IHNlZSB0aGlzPw==\"," +
"\"chainId\":\"D\"," +
"\"version\":1," + "\"signature\":\"72ddcb105778051ea2a6f92b3869e2110d50f708708a2a3fe842014c062152c8aff78dae39868d97d25831915d3c60f4acfc749dfa8bdfa395f3769d2e231a05\"" +
"}";
//Make the Post request
MultiversXUnityTools.Manager.PostRequest(url, json, CompleteMethod);
//receive the result inside Complete Method
private void CompleteMethod(OperationStatus operationStatus, string message, TransactionCostDataDto result)
{
if (operationStatus == OperationStatus.Complete)
{
Debug.Log(result.TxGasUnits);
}
else
{
Debug.LogError(message + " " + result.ReturnMessage);
}
}
Using these Get and Post methods, complex blockchain scenarios can be created. Get and Post can be used not with just the oficial APIs but with custom APIs that might be created by 3rd party developers.