Client Management
Client management is one of the core capabilities provided by AlgoKit Utils. It allows you to create (auto-retry) algod, indexer and kmd clients against various networks resolved from environment or specified configuration.
Any AlgoKit Utils function that needs one of these clients will take the underlying algosdk classes:
- algosdk.Algodv2
- algosdk.Indexer
- algosdk.Kmd
So inline with the Modularity principle you can use existing logic to get instances of these clients without needing to use the Client management capability if you prefer, including use of libraries like useWallet that have their own configuration mechanism.
Client Manager
The ClientManager is a class that is used to manage client instances.
To get an instance of ClientManager you can get it from either AlgorandClient via algorand.client or instantiate it directly:
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager';
const clientManager = new ClientManager({ algod: algodClient }); // Algod client onlyconst clientManager = new ClientManager({  algod: algodClient,  indexer: indexerClient,  kmd: kmdClient,}); // All clientsconst clientManager = new ClientManager({ algodConfig }); // Algod config onlyconst clientManager = new ClientManager({  algodConfig,  indexerConfig,  kmdConfig,}); // All client configsNetwork Configuration
The network configuration is specified using the AlgoClientConfig interface. This same interface is used to specify the config for algod, indexer and kmd SDK clients.
There are a number of ways to produce one of these configuration objects:
- Manually specifying an object that conforms with the interface, e.g.
{server: 'https://myalgodnode.com'}// Or with the optional values:{server: 'https://myalgodnode.com',port: 443,token: 'SECRET_TOKEN'}
- ClientManager.getConfigFromEnvironmentOrLocalNet()- Loads the Algod client config, the Indexer client config and the Kmd config from well-known environment variables or if not found then default LocalNet; this is useful to have code that can work across multiple blockchain environments (including LocalNet), without having to change
- ClientManager.getAlgodConfigFromEnvironment()- Loads an Algod client config from well-known environment variables
- ClientManager.getIndexerConfigFromEnvironment()- Loads an Indexer client config from well-known environment variables; useful to have code that can work across multiple blockchain environments (including LocalNet), without having to change
- ClientManager.getAlgoNodeConfig(network, config)- Loads an Algod or indexer config against AlgoNode free tier to either MainNet or TestNet
- ClientManager.getDefaultLocalNetConfig(configOrPort)- Loads an Algod, Indexer or Kmd config against LocalNet using the default configuration
Clients
Creating an SDK Client Instance
Once you have the configuration for a client, to get a new client you can use the following functions:
- ClientManager.getAlgoClient(config)- Returns an Algod client for the given configuration; the client automatically retries on transient HTTP errors
- ClientManager.getIndexerClient(config, overrideIntDecoding)- Returns an Indexer client for given configuration
- ClientManager.getKmdClient(config)- Returns a Kmd client for the given configuration
You can also shortcut needing to write the likes of ClientManager.getAlgoClient(ClientManager.getAlgodConfigFromEnvironment()) with environment shortcut methods:
- ClientManager.getAlgodClientFromEnvironment(config)- Returns an Algod client by loading the config from environment variables
- ClientManager.getIndexerClientFromEnvironment(config)- Returns an indexer client by loading the config from environment variables
- ClientManager.getKmdClientFromEnvironment(config)- Returns a kmd client by loading the config from environment variables
Accessing SDK Clients via ClientManager Instance
Once you have a ClientManager instance, you can access the SDK clients for the various Algorand APIs from it (expressed here as algorand.client to denote the syntax via an AlgorandClient):
const algorand = AlgorandClient.defaultLocalNet();
const algodClient = algorand.client.algod;const indexerClient = algorand.client.indexer;const kmdClient = algorand.client.kmd;If the method to create the ClientManager doesn’t configure indexer or kmd (both of which are optional), then accessing those clients will trigger an error to be thrown:
const algorand = AlgorandClient.fromClients({ algod });
const algodClient = algorand.client.algod; // OKalgorand.client.indexer; // Throws erroralgorand.client.kmd; // Throws errorCreating an App Client Instance
See how to create app clients via ClientManager via AlgorandClient.
Creating a TestNet Dispenser API Client Instance
You can also create a TestNet dispenser API client instance from ClientManager too.
Automatic Retry
When receiving an Algod or Indexer client from AlgoKit Utils, it will be a special wrapper client that handles retrying transient failures. This is done via the AlgoHttpClientWithRetry class.
Network Information
To get information about the current network you are connected to, you can use the network() method on ClientManager or the is{Network}() methods (which in turn call network()) as shown below (expressed here as algorand.client to denote the syntax via an AlgorandClient):
const algorand = AlgorandClient.defaultLocalNet();
const { isTestNet, isMainNet, isLocalNet, genesisId, genesisHash } =  await algorand.client.network();const testNet = await algorand.client.isTestNet();const mainNet = await algorand.client.isMainNet();const localNet = await algorand.client.isLocalNet();The first time network() is called it will make a HTTP call to algod to get the network parameters, but from then on it will be cached within that ClientManager instance for subsequent calls.