Skip to content

HttpAgent

Defined in: packages/core/src/agent/agent/http/index.ts:295

A HTTP agent allows users to interact with a client of the internet computer using the available methods. It exposes an API that closely follows the public view of the internet computer, and is not intended to be exposed directly to the majority of users due to its low-level interface. There is a pipeline to apply transformations to the request before sending it to the client. This is to decouple signature, nonce generation and other computations so that this class can stay as simple as possible while allowing extensions.

new HttpAgent(options): HttpAgent

Defined in: packages/core/src/agent/agent/http/index.ts:336

HttpAgentOptions = {}

Options for the HttpAgent

HttpAgent

Use HttpAgent.create or HttpAgent.createSync instead

readonly _isAgent: true = true

Defined in: packages/core/src/agent/agent/http/index.ts:319


config: HttpAgentOptions = {}

Defined in: packages/core/src/agent/agent/http/index.ts:320


readonly host: URL

Defined in: packages/core/src/agent/agent/http/index.ts:309


log: ObservableLog

Defined in: packages/core/src/agent/agent/http/index.ts:322


rootKey: null | Uint8Array<ArrayBufferLike>

Defined in: packages/core/src/agent/agent/http/index.ts:296

Agent.rootKey

protected _transform(request): Promise<HttpAgentRequest>

Defined in: packages/core/src/agent/agent/http/index.ts:1511

HttpAgentRequest

Promise<HttpAgentRequest>


addTransform(type, fn, priority): void

Defined in: packages/core/src/agent/agent/http/index.ts:453

"query" | "update"

HttpAgentRequestTransformFn

number = ...

void


call(canisterId, options, identity?): Promise<SubmitResponse>

Defined in: packages/core/src/agent/agent/http/index.ts:496

Makes a call to a canister method.

The ID of the canister to call. Can be a Principal or a string.

string | Principal

Options for the call.

Uint8Array

The argument to pass to the method, as a Uint8Array.

boolean

(Optional) Whether to use synchronous call mode. Defaults to true.

string | Principal

(Optional) The effective canister ID, if different from the target canister ID.

string

The name of the method to call.

Uint8Array<ArrayBufferLike> | Nonce

(Optional) A unique nonce for the request. If provided, it will override any nonce set by transforms.

(Optional) The identity to use for the call. If not provided, the agent’s current identity will be used.

Identity | Promise<Identity>

Promise<SubmitResponse>

A promise that resolves to the response of the call, including the request ID and response details.

Agent.call


createReadStateRequest(fields, identity?): Promise<any>

Defined in: packages/core/src/agent/agent/http/index.ts:1069

Create the request for the read state call. readState uses this internally. Useful to avoid signing the same request multiple times.

ReadStateOptions

Identity | Promise<Identity>

Promise<any>

Agent.createReadStateRequest


fetchRootKey(): Promise<Uint8Array<ArrayBufferLike>>

Defined in: packages/core/src/agent/agent/http/index.ts:1403

By default, the agent is configured to talk to the main Internet Computer, and verifies responses using a hard-coded public key.

This function will instruct the agent to ask the endpoint for its public key, and use that instead. This is required when talking to a local test instance, for example.

Only use this when you are not talking to the main Internet Computer, otherwise you are prone to man-in-the-middle attacks! Do not call this function by default.

Promise<Uint8Array<ArrayBufferLike>>

Agent.fetchRootKey


fetchSubnetKeys(canisterId): Promise<SubnetNodeKeys>

Defined in: packages/core/src/agent/agent/http/index.ts:1452

string | Principal

Promise<SubnetNodeKeys>


getPrincipal(): Promise<Principal>

Defined in: packages/core/src/agent/agent/http/index.ts:477

Returns the principal ID associated with this agent (by default). It only shows the principal of the default identity in the agent, which is the principal used when calls don’t specify it.

Promise<Principal>

Agent.getPrincipal


getSubnetIdFromCanister(canisterId): Promise<Principal>

Defined in: packages/core/src/agent/agent/http/index.ts:1494

Returns the subnet ID for a given canister ID, by looking at the certificate delegation returned by the canister’s state obtained by requesting the /time path with readState.

The canister ID to get the subnet ID for.

string | Principal

Promise<Principal>

The subnet ID for the given canister ID.


getTimeDiffMsecs(): number

Defined in: packages/core/src/agent/agent/http/index.ts:1532

Returns the time difference in milliseconds between the IC network clock and the client’s clock, after the clock has been synced.

If the time has not been synced, returns 0.

number


hasSyncedTime(): boolean

Defined in: packages/core/src/agent/agent/http/index.ts:1539

Returns true if the time has been synced at least once with the IC network, false otherwise.

boolean


invalidateIdentity(): void

Defined in: packages/core/src/agent/agent/http/index.ts:1444

If an application needs to invalidate an identity under certain conditions, an Agent may expose an invalidateIdentity method. Invoking this method will set the inner identity used by the Agent to null.

A use case for this would be - after a certain period of inactivity, a secure application chooses to invalidate the identity of any HttpAgent instances. An invalid identity can be replaced by Agent.replaceIdentity

void

Agent.invalidateIdentity


isLocal(): boolean

Defined in: packages/core/src/agent/agent/http/index.ts:448

boolean


parseTimeFromResponse(response): number

Defined in: packages/core/src/agent/agent/http/index.ts:1224

Uint8Array

number


query(canisterId, fields, identity?): Promise<ApiQueryResponse>

Defined in: packages/core/src/agent/agent/http/index.ts:877

Send a query call to a canister. See the interface spec.

The Principal of the Canister to send the query to. Sending a query to the management canister is not supported (as it has no meaning from an agent).

string | Principal

QueryFields

Sender principal to use when sending the query.

Identity | Promise<Identity>

Promise<ApiQueryResponse>

The response from the replica. The Promise will only reject when the communication failed. If the query itself failed but no protocol errors happened, the response will be of type QueryResponseRejected.

Agent.query


readState(canisterId, fields, _identity?, request?): Promise<ReadStateResponse>

Defined in: packages/core/src/agent/agent/http/index.ts:1105

Send a read state query to the replica. This includes a list of paths to return, and will return a Certificate. This will only reject on communication errors, but the certificate might contain less information than requested.

string | Principal

ReadStateOptions

Identity | Promise<Identity>

any

The request to send in case it has already been created.

Promise<ReadStateResponse>

Agent.readState


readSubnetState(subnetId, options): Promise<ReadStateResponse>

Defined in: packages/core/src/agent/agent/http/index.ts:1155

Reads the state of a subnet from the /api/v3/subnet/{subnetId}/read_state endpoint.

The ID of the subnet to read the state of. If you have a canister ID, you can use getSubnetIdFromCanister to get the subnet ID.

string | Principal

ReadStateOptions

The options for the read state request.

Promise<ReadStateResponse>

The response from the read state request.


replaceIdentity(identity): void

Defined in: packages/core/src/agent/agent/http/index.ts:1448

If an application needs to replace an identity under certain conditions, an Agent may expose a replaceIdentity method. Invoking this method will set the inner identity used by the Agent to a newly provided identity.

A use case for this would be - after authenticating using @icp-sdk/auth/client, you can replace the AnonymousIdentity of your Actor with a DelegationIdentity.

Actor.agentOf(defaultActor).replaceIdentity(await authClient.getIdentity());

Identity

void

Agent.replaceIdentity


status(): Promise<JsonObject>

Defined in: packages/core/src/agent/agent/http/index.ts:1384

Query the status endpoint of the replica. This normally has a few fields that corresponds to the version of the replica, its root public key, and any other information made public.

Promise<JsonObject>

A JsonObject that is essentially a record of fields from the status endpoint.

Agent.status


syncTime(canisterIdOverride?): Promise<void>

Defined in: packages/core/src/agent/agent/http/index.ts:1264

Allows agent to sync its time with the network. Can be called during initialization or mid-lifecycle if the device’s clock has drifted away from the network time. This is necessary to set the Expiry for a request

Principal

Pass a canister ID if you need to sync the time with a particular subnet. Uses the ICP ledger canister by default.

Promise<void>


syncTimeWithSubnet(subnetId): Promise<void>

Defined in: packages/core/src/agent/agent/http/index.ts:1327

Allows agent to sync its time with the network.

Principal

Pass the subnet ID you need to sync the time with.

Promise<void>


static create(options): Promise<HttpAgent>

Defined in: packages/core/src/agent/agent/http/index.ts:423

HttpAgentOptions = {}

Promise<HttpAgent>


static createSync(options): HttpAgent

Defined in: packages/core/src/agent/agent/http/index.ts:419

HttpAgentOptions = {}

HttpAgent


static from(agent): Promise<HttpAgent>

Defined in: packages/core/src/agent/agent/http/index.ts:429

V1HttpAgentInterface | Pick<HttpAgent, "config">

Promise<HttpAgent>