Algorand JavaScript SDK
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
-
TypeError: algosdk.Algodv2 is not a constructor
cause Attempting to instantiate Algodv2 via a namespace or default import/require object after migrating to v3.x.fixChange your import statement to `import { Algodv2 } from 'algosdk';` and then use `new Algodv2(...)` directly. -
ReferenceError: require is not defined
cause Using CommonJS `require()` syntax in an ES module context (`type: 'module'` in package.json or `.mjs` file).fixSwitch to ES module import syntax: `import { SomeSymbol } from 'algosdk';`. If you must use CommonJS, ensure your environment is configured for CJS (e.g., using `.cjs` extension or `type: 'commonjs'`). -
Property 'status' does not exist on type 'Algodv2' or similar TypeScript errors related to missing methods/properties.
cause Using an outdated TypeScript version (older than 4.2) that may not correctly process the SDK's bundled type definitions.fixUpdate your TypeScript compiler to version 4.2 or higher. Ensure `algosdk` is correctly installed and its types are being picked up by your `tsconfig.json`.
Warnings
- breaking Version 3.x introduces significant breaking changes from v2.x. The most notable change is the shift from a default export/namespace object to mandatory named imports for all classes and functions. Code relying on `algosdk.Algodv2` or `const algosdk = require('algosdk');` will break.
- deprecated The v2.x series of algosdk is in maintenance mode and will reach End-of-Life at the end of March 2025. No new features are being added, and after March 2025, no further updates (including security fixes) will be provided.
- gotcha This package requires Node.js version 18.0.0 or higher. Running it with older Node.js versions may lead to unexpected errors or module resolution issues, especially concerning ESM compatibility.
- gotcha While algosdk ships with TypeScript types, proper usage requires TypeScript version 4.2 or higher. Using an older TypeScript compiler might result in type errors or incorrect type inference.
Install
-
npm install algosdk -
yarn add algosdk -
pnpm add algosdk
Imports
- Algodv2
import algosdk from 'algosdk'; const client = new algosdk.Algodv2(...);
import { Algodv2 } from 'algosdk'; - makePaymentTxnWithSuggestedParams
const algosdk = require('algosdk'); const txn = algosdk.makePaymentTxnWithSuggestedParams(...);import { makePaymentTxnWithSuggestedParams } from 'algosdk'; - Transaction
import * as algosdk from 'algosdk'; const txn = new algosdk.Transaction(...);
import { Transaction } from 'algosdk';
Quickstart
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);
}
})();