Algorand JavaScript SDK

3.5.2 · active · verified Sun Apr 19

algosdk is the official JavaScript library for interacting with the Algorand blockchain network. It provides comprehensive functionalities for account management, transaction creation, signing, and submission, as well as querying blockchain data. Designed for both Node.js (requiring v18.0.0 or higher) and modern browsers, the SDK is actively maintained by Algorand, with frequent releases addressing bugfixes, enhancements, and API updates to align with the latest Algorand protocol specifications. The current stable version is 3.5.2. Version 3.x introduced significant breaking changes compared to v2.x, notably requiring explicit named imports for all functions and classes instead of a single default export or namespace object. It also ships with first-class TypeScript support, requiring TypeScript version 4.2 or higher for optimal usage.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Algorand SDK client, fetches node status, retrieves suggested transaction parameters, and optionally fetches account information.

import { Algodv2, modelsv2 } from 'algosdk';

// Configure your Algorand client using environment variables
const token = process.env.ALGORAND_ALGOD_TOKEN ?? ''; // Algod API token
const server = process.env.ALGORAND_ALGOD_SERVER ?? 'http://127.0.0.1'; // Algod server address
const port = process.env.ALGORAND_ALGOD_PORT ? parseInt(process.env.ALGORAND_ALGOD_PORT, 10) : 4001; // Algod port

const client = new Algodv2(token, server, port);

(async () => {
  try {
    console.log("Fetching Algorand node status...");
    // Fetch and log the current Algorand node status
    const status: modelsv2.NodeStatusResponse = await client.status().do();
    console.log("Node last round:", status.lastRound);

    // Fetch and log suggested transaction parameters, essential for building transactions
    const suggestedParams = await client.getTransactionParams().do();
    console.log("Suggested transaction parameters:", suggestedParams);

    // Example: Fetch account information
    // Replace 'YOUR_ALGORAND_ADDRESS_HERE' with a valid Algorand address or set via environment variable
    const testAddress = process.env.ALGORAND_TEST_ACCOUNT_ADDRESS ?? 'YOUR_ALGORAND_ADDRESS_HERE'; 
    if (testAddress !== 'YOUR_ALGORAND_ADDRESS_HERE') {
      console.log(`\nFetching account info for ${testAddress}...`);
      const accountInfo = await client.accountInformation(testAddress).do();
      console.log("Account balance (microAlgos):", accountInfo.amount);
      console.log("Account assets:", accountInfo.assets?.length || 0);
    } else {
      console.warn("\nSkipping account info fetch. Set ALGORAND_TEST_ACCOUNT_ADDRESS to a valid address to enable this.");
    }
  } catch (e) {
    console.error("An error occurred during quickstart execution:", e);
  }
})();

view raw JSON →