ClientManager
Defined in: client.ts:45
ClientManager is a central class for managing the Internet Computer (IC) agent.
It initializes the agent (connecting to local or mainnet) and integrates with TanStack Query’s QueryClient for state management. Use this as a singleton shared by all reactors in an app.
Examples
Section titled “Examples”import { ClientManager } from "@ic-reactor/core";import { QueryClient } from "@tanstack/query-core";
const queryClient = new QueryClient();const clientManager = new ClientManager({ queryClient, agentOptions: { host: "http://127.0.0.1:4943" },});
await clientManager.initialize();// Reuse the same ClientManager across multiple canistersconst backend = new Reactor<BackendService>({ clientManager, idlFactory: backendIdl, name: "backend" })const ledger = new Reactor<LedgerService>({ clientManager, idlFactory: ledgerIdl, name: "ledger" })Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ClientManager(
parameters):ClientManager
Defined in: client.ts:70
Creates a new instance of ClientManager.
Parameters
Section titled “Parameters”parameters
Section titled “parameters”Configuration options for the agent and network environment.
Returns
Section titled “Returns”ClientManager
Properties
Section titled “Properties”queryClient
Section titled “queryClient”queryClient:
QueryClient
Defined in: client.ts:58
The TanStack QueryClient used for managing cached canister data and invalidating queries on identity changes.
agentState
Section titled “agentState”agentState:
AgentState
Defined in: client.ts:62
Current state of the HttpAgent, including initialization status, network, and error information.
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get agent():
HttpAgent
Defined in: client.ts:235
The underlying HttpAgent managed by this class.
Returns
Section titled “Returns”HttpAgent
agentHost
Section titled “agentHost”Get Signature
Section titled “Get Signature”get agentHost():
URL|undefined
Defined in: client.ts:242
The host URL of the current IC agent.
Returns
Section titled “Returns”URL | undefined
agentHostName
Section titled “agentHostName”Get Signature
Section titled “Get Signature”get agentHostName():
string
Defined in: client.ts:249
The hostname of the current IC agent.
Returns
Section titled “Returns”string
trustsEnvConfig
Section titled “trustsEnvConfig”Get Signature
Section titled “Get Signature”get trustsEnvConfig():
boolean
Defined in: client.ts:267
Whether the configuration carried by the ic_env cookie may be trusted for
this agent’s host.
true when BOTH the agent host and the page origin are unambiguously a
local replica, or when the caller passed allowEnvConfig. The page counts
because the page is what decides who can write the cookie.
Every consumer of that cookie reads this one decision, so the root key, the
Internet Identity provider and a reactor’s canister ID cannot disagree
about whether the environment is trustworthy.
The deprecated allowEnvRootKey is not enough on its own: it granted the
root key alone, and is honoured for the root key alone.
Returns
Section titled “Returns”boolean
isLocal
Section titled “isLocal”Get Signature
Section titled “Get Signature”get isLocal():
boolean
Defined in: client.ts:274
Returns true if the agent is connecting to a local environment.
Returns
Section titled “Returns”boolean
network
Section titled “network”Get Signature
Section titled “Get Signature”get network():
"local"|"remote"|"ic"
Defined in: client.ts:281
Returns the current network type (‘ic’ or ‘local’).
Returns
Section titled “Returns”"local" | "remote" | "ic"
identity
Section titled “identity”Get Signature
Section titled “Get Signature”get identity():
Identity|undefined
Defined in: client.ts:293
The identity currently installed on the agent, if one was set explicitly.
Calls capture this at submit time and pass it back on every request they make, so a sign-in or sign-out part-way through cannot re-sign a request that is already in flight.
Returns
Section titled “Returns”Identity | undefined
Methods
Section titled “Methods”initialize()
Section titled “initialize()”initialize():
Promise<ClientManager>
Defined in: client.ts:182
Orchestrates the complete initialization of the ClientManager. This method awaits the agent’s core initialization (e.g., fetching root keys) Authentication session restoration is handled by AuthenticationManager.
Returns
Section titled “Returns”Promise<ClientManager>
A promise that resolves to the ClientManager instance when core initialization is complete.
initializeAgent()
Section titled “initializeAgent()”initializeAgent():
Promise<void>
Defined in: client.ts:193
Specifically initializes the HttpAgent. On local networks, this includes fetching the root key for certificate verification.
Returns
Section titled “Returns”Promise<void>
A promise that resolves when the agent is fully initialized.
getUserPrincipal()
Section titled “getUserPrincipal()”getUserPrincipal():
Promise<Principal>
Defined in: client.ts:300
Returns the current user’s Principal identity.
Returns
Section titled “Returns”Promise<Principal>
registerCanisterId()
Section titled “registerCanisterId()”registerCanisterId(
canisterId,name?):void
Defined in: client.ts:308
Registers a canister ID that this agent will interact with. This is used for informational purposes and network detection.
Parameters
Section titled “Parameters”canisterId
Section titled “canisterId”string
string
Returns
Section titled “Returns”void
connectedCanisterIds()
Section titled “connectedCanisterIds()”connectedCanisterIds():
string[]
Defined in: client.ts:331
Returns a list of all canister IDs registered with this agent.
Returns
Section titled “Returns”string[]
getSubnetIdFromCanister()
Section titled “getSubnetIdFromCanister()”getSubnetIdFromCanister(
canisterId):Promise<Principal>
Defined in: client.ts:338
Get the subnet ID for a canister.
Parameters
Section titled “Parameters”canisterId
Section titled “canisterId”string
Returns
Section titled “Returns”Promise<Principal>
syncTimeWithSubnet()
Section titled “syncTimeWithSubnet()”syncTimeWithSubnet(
subnetId):Promise<void>
Defined in: client.ts:345
Sync time with a specific subnet.
Parameters
Section titled “Parameters”subnetId
Section titled “subnetId”Principal
Returns
Section titled “Returns”Promise<void>
subscribe()
Section titled “subscribe()”subscribe(
callback): () =>void
Defined in: client.ts:354
Subscribes to identity changes (e.g., after login/logout).
Parameters
Section titled “Parameters”callback
Section titled “callback”(identity) => void
Function called with the new identity.
Returns
Section titled “Returns”An unsubscribe function.
() => void
subscribeAgentState()
Section titled “subscribeAgentState()”subscribeAgentState(
callback): () =>void
Defined in: client.ts:368
Subscribes to changes in the agent’s initialization state.
Parameters
Section titled “Parameters”callback
Section titled “callback”(state) => void
Function called with the updated agent state.
Returns
Section titled “Returns”An unsubscribe function.
() => void
updateAgent()
Section titled “updateAgent()”updateAgent(
identity):void
Defined in: client.ts:381
Replaces the current agent’s identity and invalidates TanStack queries.
Parameters
Section titled “Parameters”identity
Section titled “identity”Identity
The new identity to use.
Returns
Section titled “Returns”void