Skip to content

Algorand Client

← Back to Examples Overview

High-level AlgorandClient API for simplified blockchain interactions.

ExampleDescription
Client Instantiation

This example demonstrates the different ways to create an AlgorandClient instance:

  • AlgorandClient.defaultLocalNet() for local development
  • AlgorandClient.testNet() for TestNet connection
  • AlgorandClient.mainNet() for MainNet connection
  • AlgorandClient.fromEnvironment() reading from environment variables
  • AlgorandClient.fromConfig() with explicit AlgoConfig object
  • AlgorandClient.fromClients() with pre-configured algod/indexer/kmd clients
  • Verifying connection by calling algod.status()
AlgoAmount Utility

This example demonstrates how to use the AlgoAmount utility class to work with ALGO and microALGO amounts safely, avoiding floating point precision issues.

Topics covered:

  • Creating AlgoAmount using static factory methods
  • Accessing values in ALGO and microALGO
  • String formatting
  • Arithmetic operations (addition, subtraction)
  • Comparison operations
  • Using AlgoAmount with payment transactions
  • Avoiding floating point precision issues
Signer Configuration

This example demonstrates how to configure transaction signers on AlgorandClient:

  • setDefaultSigner() to set a fallback signer for all transactions
  • setSignerFromAccount() to register a signer from an Account object
  • setSigner() to register a signer for a specific address
  • How signers are automatically used when sending transactions
  • Registering multiple signers for different accounts
  • How the default signer is used when no specific signer is registered
  • getSigner() and getAccount() to retrieve previously registered signers
Suggested Params Configuration

This example demonstrates how to configure suggested transaction parameters:

  • setDefaultValidityWindow() to set the number of rounds a transaction is valid
  • setSuggestedParamsCache() to enable/disable caching
  • setSuggestedParamsCacheTimeout() to set cache duration in milliseconds
  • getSuggestedParams() to manually fetch suggested params
  • Performance benefits of caching when sending multiple transactions
Account Manager

This example demonstrates how to use the account manager to create, import, and manage accounts including:

  • algorand.account.random() to generate a new random account
  • algorand.account.fromMnemonic() to import from 25-word mnemonic
  • algorand.account.fromEnvironment() to load from env var
  • algorand.account.fromKmd() to get account from KMD wallet
  • algorand.account.multisig() to create a multisig account
  • algorand.account.logicsig() to create a logic signature account
  • algorand.account.rekeyed() to create a rekeyed account reference
  • algorand.account.getInformation() to fetch account details from network
  • algorand.account.ensureFunded() to ensure account has minimum balance
  • algorand.account.ensureFundedFromEnvironment() for dispenser funding
Send Payment

This example demonstrates how to send ALGO payment transactions:

  • algorand.send.payment() with basic parameters (sender, receiver, amount)
  • Using AlgoAmount for the amount parameter
  • Payment with note field
  • Payment with closeRemainderTo to close account and send remaining balance
  • Understanding the SendSingleTransactionResult return value
  • Displaying transaction ID and confirmed round
  • Verifying balances before and after payment
Send Asset Operations

This example demonstrates how to perform ASA (Algorand Standard Asset) operations:

  • algorand.send.assetCreate() to create a new ASA with all parameters
  • algorand.send.assetConfig() to reconfigure an asset
  • algorand.send.assetOptIn() for receiver to opt into the asset
  • algorand.send.assetTransfer() to transfer assets between accounts
  • algorand.send.assetFreeze() to freeze/unfreeze an account’s asset holding
  • algorand.send.assetTransfer() with clawbackTarget for clawback operations
  • algorand.send.assetOptOut() to opt out and close asset holding
  • algorand.send.assetDestroy() to destroy an asset
Send Application Operations

This example demonstrates how to perform smart contract (application) operations:

  • algorand.send.appCreate() to deploy a new application with global/local schema
  • algorand.send.appUpdate() to update application code
  • algorand.send.appCall() for NoOp application calls with args
  • algorand.send.appCall() with OnApplicationComplete.OptIn for account opt-in
  • algorand.send.appCall() with OnApplicationComplete.CloseOut for account close-out
  • algorand.send.appCall() with OnApplicationComplete.ClearState to clear local state
  • algorand.send.appDelete() to delete the application
  • Passing application arguments, accounts, assets, apps references
Create Transaction (Unsigned Transactions)

This example demonstrates how to create unsigned transactions without immediately sending them, which is useful for:

  • Transaction inspection and debugging
  • Multi-party signing workflows
  • Custom signing flows (hardware wallets, HSMs, etc.)
  • Modifying transaction fields before signing
  • Building transaction groups for atomic transactions

Key concepts:

  • algorand.createTransaction.payment() creates unsigned payment transactions
  • algorand.createTransaction.assetCreate() creates unsigned asset creation
  • algorand.createTransaction.assetTransfer() creates unsigned asset transfers
  • algorand.createTransaction.appCall() creates unsigned app calls
  • Transaction objects have properties like txId(), fee, firstValid, lastValid
  • Manual signing with account.signer() function
  • Sending signed transactions via algorand.client.algod.sendRawTransaction()
Transaction Composer (Atomic Transaction Groups)

This example demonstrates how to build atomic transaction groups using the transaction composer:

  • algorand.newGroup() creates a new transaction composer
  • Adding multiple transactions using .addPayment(), .addAssetOptIn(), etc.
  • Method chaining: algorand.newGroup().addPayment(…).addPayment(…)
  • .simulate() to simulate the transaction group before sending
  • .send() to execute the atomic transaction group
  • Atomicity: all transactions succeed or fail together
  • Adding transactions with different signers
  • Group ID assigned to all transactions in the group
Asset Manager

This example demonstrates the AssetManager functionality for querying asset information and performing bulk opt-in/opt-out operations:

  • algorand.asset.getById() to fetch asset information by asset ID
  • algorand.asset.getAccountInformation() to get an account’s asset holding
  • algorand.asset.bulkOptIn() to opt into multiple assets at once
  • algorand.asset.bulkOptOut() to opt out of multiple assets at once
  • Efficiency comparison: bulk operations vs individual opt-ins
  • Error handling for non-existent assets and non-opted-in accounts
App Manager

This example demonstrates the AppManager functionality for querying application information, state, box storage, and TEAL compilation:

  • algorand.app.getById() to fetch application information
  • algorand.app.getGlobalState() to read global state
  • algorand.app.getLocalState() to read account’s local state
  • algorand.app.getBoxNames() to list all box names for an app
  • algorand.app.getBoxValue() to read a specific box value
  • algorand.app.getBoxValues() to read multiple box values
  • algorand.app.getBoxValuesFromABIType() to decode typed box values
  • algorand.app.compileTeal() to compile TEAL source code
  • algorand.app.compileTealTemplate() to compile TEAL with template variables
App Deployer

This example demonstrates the AppDeployer functionality for idempotent application deployment with create, update, and replace strategies:

  • algorand.appDeployer.deploy() for initial deployment
  • Deploy parameters: name, version, approvalProgram, clearProgram, schema, onUpdate, onSchemaBreak
  • Idempotency: calling deploy() again with same version does nothing
  • onUpdate: ‘update’ to update existing app when version changes
  • onUpdate: ‘replace’ to delete and recreate app when version changes
  • onUpdate: ‘fail’ to fail if app already exists with different code
  • onSchemaBreak: ‘replace’ when schema changes require new app
  • Deployment metadata stored in app global state
  • App name used for idempotent lookups
Client Manager

This example demonstrates how to access the underlying raw clients through the ClientManager (algorand.client), and how to get typed app clients:

  • algorand.client.algod - Access the raw Algod client
  • algorand.client.indexer - Access the raw Indexer client
  • algorand.client.kmd - Access the raw KMD client
  • algorand.client.indexerIfPresent - Safely access Indexer (returns undefined if not configured)
  • algorand.client.getAppClientById() - Get typed app client by ID
  • algorand.client.getAppClientByCreatorAndName() - Get typed app client by creator/name
  • algorand.client.getAppFactory() - Get app factory for creating/deploying apps
  • When to use raw clients vs AlgorandClient methods
Error Transformers

This example demonstrates how to register custom error transformers to enhance error messages and debugging information for failed transactions:

  • What error transformers are and why they’re useful
  • algorand.registerErrorTransformer() to add custom error transformers
  • Creating transformers that add source code context to logic errors
  • Creating transformers that provide user-friendly error messages
  • How transformers receive errors and can return enhanced errors
  • algorand.unregisterErrorTransformer() to remove transformers
  • Triggering intentional errors and showing enhanced output
  • How multiple transformers can be chained
  • The transformer function signature: (error: Error) => Promise<Error>
Transaction Leases

This example demonstrates how to use transaction leases to prevent duplicate transactions within a validity window:

  • Sending a payment with a string lease
  • Demonstrating duplicate rejection when reusing the same lease
  • Sending a payment with a Uint8Array lease (32 bytes)

A lease enforces a mutually exclusive transaction for a given sender within the validity window. See the lease param in src/transactions/common.ts:28 and encodeLease() in src/transaction/transaction.ts:23.

Run any example from the repository’s examples directory:

Terminal window
cd examples
npm run example algorand_client/01-*.ts