What is algosdk?
The algosdk npm package is a JavaScript library for interacting with the Algorand blockchain. It provides tools for building applications on Algorand, including creating and managing accounts, sending transactions, and interacting with smart contracts.
What are algosdk's main functionalities?
Account Management
This feature allows you to create and manage Algorand accounts. The code sample demonstrates how to generate a new account and retrieve its address and mnemonic.
const algosdk = require('algosdk');
const account = algosdk.generateAccount();
console.log('Address:', account.addr);
console.log('Mnemonic:', algosdk.secretKeyToMnemonic(account.sk));
Transaction Handling
This feature enables the creation and submission of transactions on the Algorand blockchain. The code sample shows how to create a payment transaction, sign it, and send it to the network.
const algosdk = require('algosdk');
const algodClient = new algosdk.Algodv2(token, server, port);
const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: senderAddress,
to: receiverAddress,
amount: 1000000,
suggestedParams: await algodClient.getTransactionParams().do()
});
const signedTxn = txn.signTxn(senderPrivateKey);
const sendTx = await algodClient.sendRawTransaction(signedTxn).do();
console.log('Transaction ID:', sendTx.txId);
Smart Contract Interaction
This feature allows interaction with Algorand smart contracts. The code sample demonstrates how to call a smart contract with arguments using a NoOp transaction.
const algosdk = require('algosdk');
const algodClient = new algosdk.Algodv2(token, server, port);
const appArgs = [algosdk.encodeUint64(123)];
const txn = algosdk.makeApplicationNoOpTxn(senderAddress, await algodClient.getTransactionParams().do(), appId, appArgs);
const signedTxn = txn.signTxn(senderPrivateKey);
const sendTx = await algodClient.sendRawTransaction(signedTxn).do();
console.log('Transaction ID:', sendTx.txId);
Other packages similar to algosdk
web3
Web3.js is a JavaScript library for interacting with the Ethereum blockchain. It provides similar functionalities to algosdk, such as account management, transaction handling, and smart contract interaction, but is specific to Ethereum.
ethers
Ethers.js is another JavaScript library for Ethereum, offering a more lightweight and modular approach compared to web3.js. Like algosdk, it provides tools for managing accounts, sending transactions, and interacting with smart contracts, but is focused on the Ethereum ecosystem.
js-algorand-sdk

AlgoSDK is a javascript library for communicating with the Algorand network for modern browsers and node.js.
Installation
node.js
$ npm install algosdk
Browser
The dist
directory contains a minified version of the library - algosdk.min.js
.
Include this line in your HTML.
<script src="algosdk.min.js"/>
Quick Start
const token = "Your algod API token";
const server = "http://127.0.0.1";
const port = 8080;
const client = new algosdk.Algod(token, server, port);
(async () => {
console.log(await client.status());
})().catch(e => {
console.log(e);
});
Documentation
For detailed information about the different API calls in client
, visit https://developer.algorand.org
Usage
Generate an Algorand account
var keys = algosdk.generateAccount();
Example result
{addr: "IB3NJALXLDX5JLYCD4TMTMLVCKDRZNS4JONHMIWD6XM7DSKYR7MWHI6I7U", sk: Uint8Array(64)}
Secret key to mnemonic
var mnemonic = algosdk.secretKeyToMnemonic(keys.sk);
Example result
"gorilla fortune learn marble essay uphold defense hover index effort ice atom figure will improve mom indoor mansion people elder hill material donkey abandon gown"
Mnemonic to secret sey
var secret_key = algosdk.mnemonicToSecretKey(mnemonic);
Example result
{addr: "IB3NJALXLDX5JLYCD4TMTMLVCKDRZNS4JONHMIWD6XM7DSKYR7MWHI6I7U", sk: Uint8Array(64)}
Check the validity of an Address
var isValid = algosdk.isValidAddress("IB3NJALXLDX5JLYCD4TMTMLVCKDRZNS4JONHMIWD6XM7DSKYR7MWHI6I7U");
Example result
true
Sign a transaction
In order to create and sign a transaction, create first an object with the relevant properties.
There is no need to specify the from
address, it is computed directly from the secretKey.
Note -- The fields names must be identical to the following example's.
Note 2 -- In order to encode data into the note field, simply encode any JavaScript object using algosdk.encodeObj(o)
.
var txn = {
"to": "7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q",
"fee": 10,
"amount": 847,
"firstRound": 51,
"lastRound": 61,
"note": new Uint8Array(0),
"GenesisID":"testnet-v1.0"
};
Then, call signTransaction
and pass the transaction along with relevant private key.
var signedTxn = algosdk.signTransaction(txn, keys.sk);
Now signedTxn
can be posted to the network via algod.sendRawTransaction()
.
Master derivation key to mnemonic
var mnemonic = algosdk.masterDerivationKeyToMnemonic(mdk);
Example result
label danger traffic dream path boss runway worry awful abuse stairs spare wasp clock steel impact swear eagle canal diagram nation upon creek abstract pride
Mnemonic to master derivation key
var mdk = algosdk.mnemonicToMasterDerivationKey(mnemonic);
Example result
Uint8Array(32)
Sign a bid
Bids have similar pattern to a transaction.
First, create an object with the bid's information
var bid = {
"bidderKey": "IB3NJALXLDX5JLYCD4TMTMLVCKDRZNS4JONHMIWD6XM7DSKYR7MWHI6I7U",
"auctionKey": "7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q",
"bidAmount": 1000,
"maxPrice": 10,
"bidID": 2,
"auctionID": 56
};
Then, call signBid
and pass the bid and information along with the private key.
var signedBid = algosdk.signBid(bid, keys.sk);
In order to send a bid to the network. Embbed the output of algosdk.signBid
to a transaction note
's field.
For example,
var txn = {
"to": "7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q",
"fee": 10,
"amount": 0,
"firstRound": 51,
"lastRound": 61,
"note": <signedBid>
};
Contribution
For information on how to contribute, please refer to our contribution guide.
License
js-algorand-sdk is licensed under a MIT license. See the LICENSE file for details.