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 · 30 minutes

Algorand BSN Portal - Getting Started

Algorand BSN Portal provides a way to create a gateway service accessing Algorand blockchain networks (Mainnet and Testnet) on BSN (Blockchain-based Service Network). This tutorial is a step-by-step guide from opening an account on Algorand BSN Portal to creating an access gateway to Algorand Testnet. After access gateway is created, we will perform some tasks on the Algorand Testnet.

Requirements

  • nodeJS
  • cURL

Background

The overall process is:

Working with Algorand BSN Portal

  • create account
  • create project
  • test gateway access
  • check statistics on BSN Portal

Interacting with Algorand Testnet through BSN Gateway

  • prepare Node environment
  • create Algorand account
  • check account balance
  • get fund from testnet faucet
  • check account in Algorand explorer
  • get block information using indexer

Steps

Step 1: Working with Algorand BSN Portal

Step 1.1: Create Account

Login https://bsn.algorand.org
EditorImages/2021/01/16 00:52/Portal_fp.png
Use Sign up to open a new account on this portal. You will receive a confirmation email for account activation. After you activate your account, you can use it to Sign in.

Step 1.2: Create New Project

After you sign in, you will see the portal. If you first sign in and have not created any project, you will see no project yet in the portal.
EditorImages/2021/01/16 00:58/Portal_p1.png
Now you can create a new project by click New Project.

The portal provides information about BSN Node Locations where you can access Algorand and the Service Plan.
EditorImages/2021/01/16 00:59/Portal_p2.png
Here we select HongKong PCN and Free Plan to continue.

We are asked to give a name for the New Project. We can select Algorand Mainnet or Algorand Testnet here.

In this case we give name Project01 and choose Testnet.EditorImages/2021/01/16 01:01/Portal_p3.png
Click Create to continue.

Upon success, we will see our project created with detail here.EditorImages/2021/01/16 01:03/Portal_p4.png
Here we see the Access addresses for both algod and indexer REST API, which will be used later when we interact with Algorand Testnet. Note that Project Key is empty by default. When configured, this key will be placed in x-api-key.

Click More and select Update Key.EditorImages/2021/01/16 01:06/Portal_p5.png
Now we have Project Key as well in our project.
EditorImages/2021/01/16 01:08/Portal_p6.png

Step 1.3: Test Gateway Access

To check if the gateway is working correctly, we can fetch some information on Algorand Testnet with this gateway. Here we simply use CURL. (Here you can find more about V2 API: https://developer.algorand.org/docs/reference/rest-apis/algod/v2/)

First let’s try using access address WITHOUT the key.

curl https://hk.bsngate.com/api/6d6a4e6e2c41dbfff5cf691f082b05a10f95a8dfd3d4cacad5b67dcffed5d274/Algorand-Testnet/algodrest/v2/status

EditorImages/2021/01/16 01:13/curl_p1.png

Without the project key, we are not authorized to access the gateway.

Now we place our key as the header x-api-key.

curl -H "x-api-key:f761508095214066fafe96391b4cb6cd71b3e0fc66caaf3f4404c53b3ed24f42" https://hk.bsngate.com/api/6d6a4e6e2c41dbfff5cf691f082b05a10f95a8dfd3d4cacad5b67dcffed5d274/Algorand-Testnet/algodrest/v2/status

EditorImages/2021/01/16 01:15/curl_p2.png
We see the gateway to Algorand Testnet is working well. We will not use REST API any more, as we are using JavaScript SDK in the remaining part of this tutorial.

Step 1.4: Check Statistics on Portal

Before we move to more activities on Algorand SDK, we will take a look on the statistics on Algorand BSN Portal.

Click Statistics.EditorImages/2021/01/16 01:22/Portal_stats.png

We start seeing traffics on our previous REST-API access. You can always come back to the portal and check the statistics.

Step 2: Interacting with Algorand Testnet through BSN Gateway

With the gateway through BSN ready, we now focus on basic operation on interacting with Algorand Testnet. For demonstration purpose we simply create Account in Testnet and get fund from Faucet.

Step 2.1: Prepare Node Environment

Prepare a directory as our project directory and initalize a NodeJS environment.

cd AlgorandBSNPortal
npm init -y

EditorImages/2021/01/16 01:27/npm_p1.png

Install Algorand SDK

npm install algosdk -s

EditorImages/2021/01/16 01:28/npm_p2.png

Step 2.2: Create Algorand Account

We will use this script to generate a new account. Use your own editor and create this file.

Like all other blockchain platforms, account is represented by a private key and a public address. The private key is always represented as 25-word mnemonic in Algorand, while address is a x-byte seen in external world. Always keep the 25-word mnemonic private as it is being used to sign transaction.

(Note: We only need the SDK to generate account. We do not specify whether we are using Algorand Mainnet or Algorand Testnet, as Algorand does not distinguish accounts between them. Meanwhile, we do not need to access the gateway. Everything is just done locally in your host. And you can generate as many accounts as possible. Just choose the one you are going to use in this tutorial).

generate_account.js

const algosdk = require('algosdk');

let account = algosdk.generateAccount();
console.log("Account Address: ", account.addr);

let mn = algosdk.secretKeyToMnemonic(account.sk);
console.log("Account Mnemonic: ", mn);

EditorImages/2021/01/16 01:37/generateaccount.png
The account address we generate is TG5SZ6PF3UP7PN4CVPR5D4EX7P5UINVWRZPOEJOXACLJAAGF7ASLI32QWI. We will use it in the next steps.

Step 2.3: Check Account Balance

Now we can take a look on the account information, including account balance of our newly generated account. We are using this script. Fill in the information such as
- Access Address: taken from the project created in BSN Portal
- Key: taken from the project created in BSN Portal
- Account Address: the one we just generated in previous step

check_accountinfo.js

const algosdk=require('algosdk');

const server="<YOUR ACCESS ADDRESS>";
const port="";
const token={
    "x-api-key": "<YOUR KEY>"
};

let client=new algosdk.Algodv2(token,server,port);
let accountAddress = "YOUR ACCOUNT ADDRESS";
( async() => {
    let account_info = (await client.accountInformation(accountAddress).do());
    console.log(account_info);
})().catch(e => {
    console.log(e);
})

EditorImages/2021/01/16 01:42/accountinfo_noalgs.png
We see no Algos in this account as it is newly generated. No state information about this account is found in Algorand Testnet.

Step 2.4: Get Fund from Faucet

Now we can get some fund on this account. Go to the Testnet Faucet (https://bank.testnet.algorand.network) and paste your account there.EditorImages/2021/01/16 01:44/faucet.png
After you see Status updated, your account will be funded with 100 Algos.

We check account again with the same script.
EditorImages/2021/01/16 01:45/accountinfo_100algs.png

Step 2.5: Check Account in Algorand Explorer

Finally you can always check your account in the public ledger. There are several explorers available that you can check anything on the Testnet. For example, in https://algoexplorer.io/, we paste our account address.
EditorImages/2021/01/16 01:48/explorer.png
And we see the balance of our account is 100 Algos. There was one transaction, which is the transaction the account received Testnet faucet (note the TxID on this and the output of Faucet).

Step 2.6: Get Block Information using Indexer

Algorand BSN Portal also provides another access address, reaching the indexer API. Here is how we use indexer API to access a block.

lookup_block.js

const algosdk=require('algosdk');

const server="<YOUR ACCESS ADDRESS>";
const port="";
const token={
    "x-api-key": "<YOUR KEY>"
};

llet client = new algosdk.Indexer(token,server,port);
(async()=> {
    let blockInfo = await client.lookupBlock(10000).do()
    console.log(blockInfo)
})().catch(e => {
    console.log(e);
});

EditorImages/2021/01/16 01:51/lookupblock.png

Next Step

This is the end of this tutorial. Now you have a funded account and you can continue testing more operations and advanced features in Algorand, such as transferring Algos between accounts, or Algorand Standard Asset (ASA) through this gateway.