Connecting to Algorand: Python, Go, Javascript (examples)

Follow

Example code snippets for connecting to an algorand node in three languages: Python, Go, and Javascript.

Python:

import algosdk

# Node Access
client = algosdk.v2client.algod.AlgodClient('<your_algo_api_token>', '<your_blockdaemon_node_address>', {
'X-Auth-Token': '<your_blockdaemon_token>'
})
resp = client.status()
print(resp)

 

Go:

package main

import (
    "fmt"

    "github.com/algorand/go-algorand-sdk/client/algod"
)

const algodAddress = "<your_blockdemon_noe_address>"
const algodToken = "Your_algorand_api_token"

func main() {
    // Create an algod client
    algodClient, err := algod.MakeClientWithHeaders(algodAddress, algodToken, []*algod.Header{
        {
            Key:   "X-Auth-Token",
            Value: "<your_blockdaemon_token>",
        },
    })
    if err != nil {
        return
    }
    // Print algod status
    nodeStatus, err := algodClient.Status()
    if err != nil {
        fmt.Printf("error getting algod status: %s\n", err)
        return
    }

    fmt.Printf("algod last round: %d\n", nodeStatus.LastRound)
    fmt.Printf("algod time since last round: %d\n", nodeStatus.TimeSinceLastRound)
    fmt.Printf("algod catchup: %d\n", nodeStatus.CatchupTime)
    fmt.Printf("algod latest version: %s\n", nodeStatus.LastVersion)

 

Javascript:

import algosdk from 'algosdk'

const token = '<your algorand_api_token>';
const server = '<your_blockdasemon_node_address>';
const port = 443;
const client = new algosdk.Algod(token, server, port, {
    'X-Auth-Token': '<your blockdaemon_token>'
});

(async () => {
  console.log(await client.status());
})().catch((e) => {
  console.log(e);
});

 

Resources:

connecting to Algorand mainnet

1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.