HederaClientService API Reference
This document provides a detailed API reference for the HederaClientService
class — a service to manage Hedera SDK clients with support for multiple Hedera networks (mainnet, testnet, previewnet, local-node, and custom networks).
Types and Interfaces
NetworkName
An object to optionally specify a network name when performing operations.
-
networkName
(optional, string): The name of the Hedera network to use for the operation. If omitted and there is only one configured network, that network will be used by default.
HederaNetwork
A union type representing the allowed Hedera public networks.
-
'mainnet'
: The main Hedera public network. -
'testnet'
: The Hedera test network for testing and development. -
'previewnet'
: A preview network for early access features. -
'local-node'
: A local Hedera node network, typically used for local testing.
HederaCustomNetwork
Defines a configuration for a custom Hedera network.
-
name
(string): Unique identifier for the custom network. -
nodes
(object): A map where keys are node account IDs or names and values are node addresses (string orAccountId
). -
mirrorNodes
(optional, string | string[]): One or more mirror node URLs for the custom network.
NetworkConfig
Configuration for connecting to a Hedera network.
-
network
(HederaNetwork | HederaCustomNetwork): Either a predefined Hedera public network name or a custom network configuration. -
operatorId
(string): The account ID of the operator (payer of transactions) on the Hedera network. -
operatorKey
(string): The private key corresponding to the operator account, used for signing transactions.
HederaClientConfiguration
Top-level configuration for initializing HederaClientService
.
-
networks
(array ofNetworkConfig
): Array of network configurations supported by the client service. This array must contain at least one network, and each network must have a unique name.
-
The uniqueness of network names is validated during initialization.
-
Each network config defines important credentials and endpoints for connecting and signing on Hedera.
Class: HederaClientService
Manages Hedera SDK clients for one or more networks, providing convenience methods to obtain clients and run operations with automatic lifecycle management.
constructor
constructor(config: HederaClientConfiguration)
Creates a new instance of HederaClientService
with the specified configuration.
-
config
(required): Configuration object specifying one or more Hedera networks via an array ofNetworkConfig
.-
Must contain at least one network configuration.
-
Network names must be unique across all entries.
-
-
Throws an error if no networks are provided.
-
Throws an error if there are duplicate network names.
getClient
public getClient(networkName?: string): Client
Retrieves a Hedera Client
instance for the specified network.
-
networkName
(optional, string): The name of the network to get the client for.-
If omitted and only one network is configured, the single configured network is used.
-
If omitted but multiple networks are configured, an error is thrown.
-
The network name must match either a known public Hedera network or a custom network name.
-
-
A configured Hedera
Client
instance for the network.
-
Throws an error if the specified network name is unknown.
-
For known public networks (
mainnet
,testnet
,previewnet
,local-node
), a preconfigured client is created viaClient.forName
. -
For custom networks, the client is created from the custom configuration including nodes, mirror nodes, and operator credentials.
-
The default max transaction fee is set to 2 HBAR for all clients.
withClient
public async withClient<T>(props: NetworkName, operation: (client: Client) => Promise<T>): Promise<T>
Convenience method to execute an asynchronous operation that requires a Hedera client.
-
props
(required): An object optionally includingnetworkName
specifying which network client to use. -
operation
(required): A callback function that receives the HederaClient
instance and returns aPromise<T>
.
-
A promise resolving to the result of the
operation
.
-
Obtains a client with
getClient
using the providednetworkName
. -
Executes the
operation
callback with the client. -
Ensures the client connection is properly closed after the operation completes.