Create Publication

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Tutorial Thumbnail
Beginner · 15 minutes or less

Using PureStake API with Algo Builder

This is the eight tutorial from the Algo Builder series:

In this tutorial we present how to use PureStake API (algodv2 and indexer) within algob projects.

Requirements

  • Brief knowledge about Blockchain and Algorand.
  • Brief introduction to algob.
  • Brief knowledge about assets, accounts, transactions and signatures.

Info

Algo Builder fills a gap in the Algorand ecosystem to create and manage projects. Our goal is to make shipping Algorand applications simple, efficient, and scalable. Think about it as a Truffle suite for Algorand. For more info, check our articles in the developer portal.

Background

While you can always run your own node, using an API service to query the network is significantly faster than downloading transaction data and configuring your own infrastructure. The PureStake API-As-A-Service makes it easy to quickly get up-and-running on the Algorand network. It provides developers with easy-to-use access to native Algorand REST APIs.

Using purestake api with algob

Purestake API’s can be easily integrated with algo-builder scripts. You just need to add relevent config (host, token) in your algob.config.js. Currently purestake offers the Algorand Algod v2, and Indexer APIs via service.

Before using purestake API, as a prerequisite you need to register an account and get Your Unique API Key. For more info about the same, check this tutorial by PureStake (step1 & step2).

Note

PureStake’s API does not work with the Algorand SDK code examples - requiring an alternate header value 'X-API-Key' in place of the default 'X-Algo-API-Token' (examples shown in below section).

Steps

1. Using algodv2 PureStake endpoints with algo-builder

For algodv2, the host urls for mainnet, testnet & betanet are:
+ mainnet: https://mainnet-algorand.api.purestake.io/ps2
+ testnet: https://testnet-algorand.api.purestake.io/ps2
+ betanet: https://betanet-algorand.api.purestake.io/ps2

You will also need your Unique API KEY, which will be present in your purestake.io dashboard.

For example, say if we want to access purestake testnet API, we will add a new network configuration in algob.config.js:

const { mkAccounts } = require("@algo-builder/algob");
let accounts = mkAccounts([
  {
    name: "elon-musk",
    addr: "WHVQXVVCQAD7WX3HHFKNVUL3MOANX3BYXXMEEJEJWOZNRXJNTN7LTNPSTY",
    mnemonic: "resist derive table space jealous person pink ankle hint venture manual spawn move harbor flip cigar copy throw swap night series hybrid chest absent art"
  }
]);

let purestakeTestNetCfg = {
  host: "https://testnet-algorand.api.purestake.io/ps2",
  port: '',
  token: {
    'X-API-Key': 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab' // replace this with your API key
  },
  accounts: accounts
};

module.exports = {
  networks: {
    default: defaultNet,
    testnet: purestakeTestNetCfg
  }
};

Now while running the algob script, you can use the --network flag to run script on this network.

algob run scripts/run.js --network testnet

2. Using indexer PureStake endpoints with algo-builder

For indexer, the host urls for mainnet, testnet & betanet are:
+ mainnet: https://mainnet-algorand.api.purestake.io/idx2
+ testnet: https://testnet-algorand.api.purestake.io/idx2
+ betanet: https://betanet-algorand.api.purestake.io/idx2

Similar to above section, you can add this config in your network’s indexerCfg namespace in algob.config.js, and access the API via deployer.indexerClient in an algob script:

const purestakeIndexerCfg = {
  host: "https://testnet-algorand.api.purestake.io/idx2",
  port: '',
  token: {
    'X-API-Key': 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab' // replace this with your API key
  },
};

let defaultCfg = {
  // algod config
  host: "http://localhost",
  port: 4001,
  token: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  accounts: accounts,
  // indexer config
  indexerCfg: purestakeIndexerCfg
};

module.exports = {
  networks: {
    purestake: defaultCfg
  }
};

Similar to above, run the script passing --network purestake.

An Example code using indexer in an algob script:

async function run (runtimeEnv, deployer) {
  console.log('Script has started execution!');

  const iClient = deployer.indexerClient;
  const health = await iClient.makeHealthCheck().do();
  console.log('health: ', health);
}