Making GET and POST HTTP request to Algorand using PureStake API

Making GET and POST HTTP request to Algorand using PureStake API

Representation State Transfers (REST) is a software architecture that allows developers to make HTTP(S) requires a database to access and use data via an application programming interface (API). By using RESTFul APIs, developers can make HTTP API calls to GET, POST, READ AND DELETE (CRUD) information data in a database or file. Similarly, interacting with Algorand requires a medium through which developers can perform CRUD actions.

There are many ways of interacting and performing basic CRUD actions on the Algorand chain. To have full access to the Algorand chain, the best option is to set up a node (a server that syncs the Algorand database) to archive and store historical data. However, setting up a node on Algorand is technical and has overhead costs as well. You will need to buy server resources as well as commit human resources to manage it. For an app developer, this is not an ideal route. Thankfully, there are Algorand RESTful APIs, which provide endpoints to performing basic CRUD functions on the blockchain. You should note the following about the capabilities of the RESTFul APIs

  • They are mostly good for GET and POST, UPDATE transactions.

Currently, there are two (2) RESTFul APIs that allow interactions with the Algorand chain. They are the PureStake API and the AlgoExplorer API. The difference between these two APIs is the access rules. PuresStake API is free up to a limited API calls. Secondly, developers will need to create an account and get access to API keys in order to use PureStake. The PureStake API endpointS acts as a node thereby allowing third-parties to interact with Algorand via their APIs. This post will focus on the PureStake API. I will demonstrate basic POST and GET requests using both methods.

Requirements

1. Call or include the Algorand SDK into your Javascript code

The SDK is installed via by downloading the file and including it in your code or you can install it via NPM

require(algosdk);

2. Get Algorand Address

To send a transaction to the Algorand chain, it must be signed by an Algorand address. To make a dynamic app, users can input their mnemonic phrase to be used to recover the Algorand address to sign the transaction

var mnemonic = “wood clump category carpet cabin cement joy cover this hour armor twist write trade term only label later lizard disease boil pelican dish one test”;

//Recover the account from using the algosdk and mnemonicToSecretKey class respectively.

var recoveredAccount = algosdk.mnemonicToSecretKey(mnemonic);
The recoveredAccount is an array which has the addr containing the variable

In building the transaction array, assign recoveredAccount.addr to the address value, which will be demonstrated later.

3. Create PureStake client to get Params

On your PureStake account dashboard, there is API key for the PureStake account and the endpoints. Copy these details and replace them in the piece of code below. These become necessary to create an algosdk client to post transactions

const newServer = “https://testnet-algorand.api.purestake.io/ps2″; //define the Purestake API endpoint. Use https://mainnet-algorand.api.purestake.io/ps2; for live produciton

const port = “”; // leave this blank for now

//assign your API key to a token variable in the Jason. This API key comes from your PureStake account

const token = {

‘X-API-key’ : ‘b0r9BtPAG64Adngz6DO6e6wo4FhQenSw1OUpXLHT’,

}

4. Proceed to create the Algosdk client

let algodClientV2 = new algosdk.Algodv2(token, newServer, port);

Now we get the params from the Algorand chain

let params = await algodClientV2.getTransactionParams().do();

Now, you can access all the Algorand params needed for to process the transaction

firstRound = params.firstRound,

lastRound = params.lastRound,

genesisID = params.genesisID,

genesisHash = params.genesisHash

5. Constructing transaction details

After getting PARAMS and recoverdAddress from the mnemonic phrase, the remaining variables such as transaction amount, fee, and type are determined dynamically or statically. to field is the account that is going to receive the transaction. It will be statically set.

Below are the definitions of the variables for the transaction construction

let address = recoveredAccount.addr
let amount = 1000
let fee = 1

let txn = {

“from”: address,

“to”: “AXTW46L6EPL4V7XSJAR7X24ATUM4N43BA6HQT3IBG3RS6GZCOBJNR735NI”,

“fee”: fee,

“amount”: amount,

“firstRound”: firsRound,

“lastRound”: lastRound,

“genesisID”: genesisID,

“genesisHash”: genesisHash,

“note”: new Uint8Array(0),

“type”: “pay”,

};

Then proceed to sign the transaction. The transaction is signed using the Algosdk class and function for that. You don’t use PureStake or AlgoExplorer APIs to sign the transactions.

let signedTxn = algosdk.signTransaction(txn, recoveredAccount.sk);

The transaction is signed and assigned to the signedTxn object. The next step is to post/send this to the Algorand blockchain. This works differently for the two APIs

6. Sign the transaction

This API allows you to call promise Algorand SDK classes and functions. Remember that we initiated the algoClientV2 class object above with the Algorand endpoints and API. That will be used to send the post request using inbuild transactions from the Algorand SDK

let sendTx = await algodClientV2.sendRawTransaction(signedTxn.blob).do();

The transaction is signed by the response object assigned to the sendTx and you can access response data like the transactionID by referencing sendTx.txId

Scroll to Top