Skip to content
Still using @dfinity/agent? Migrate to @icp-sdk/core!

HttpAgent

Defined in: packages/agent/src/agent/http/index.ts:286

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/agent/src/agent/http/index.ts:327

HttpAgentOptions = {}

Options for the HttpAgent

HttpAgent

Use HttpAgent.create or HttpAgent.createSync instead

readonly _isAgent: true = true

Defined in: packages/agent/src/agent/http/index.ts:310


config: HttpAgentOptions = {}

Defined in: packages/agent/src/agent/http/index.ts:311


readonly host: URL

Defined in: packages/agent/src/agent/http/index.ts:300


log: ObservableLog

Defined in: packages/agent/src/agent/http/index.ts:313


rootKey: null | Uint8Array<ArrayBufferLike>

Defined in: packages/agent/src/agent/http/index.ts:287

Agent.rootKey

protected _transform(request): Promise<HttpAgentRequest>

Defined in: packages/agent/src/agent/http/index.ts:1369

HttpAgentRequest

Promise<HttpAgentRequest>


addTransform(type, fn, priority): void

Defined in: packages/agent/src/agent/http/index.ts:444

"query" | "update"

HttpAgentRequestTransformFn

number = ...

void


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

Defined in: packages/agent/src/agent/http/index.ts:487

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/agent/src/agent/http/index.ts:1053

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/agent/src/agent/http/index.ts:1302

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<undefined | SubnetStatus>

Defined in: packages/agent/src/agent/http/index.ts:1351

string | Principal

Promise<undefined | SubnetStatus>


getPrincipal(): Promise<Principal>

Defined in: packages/agent/src/agent/http/index.ts:468

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


getTimeDiffMsecs(): number

Defined in: packages/agent/src/agent/http/index.ts:1390

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/agent/src/agent/http/index.ts:1397

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

boolean


invalidateIdentity(): void

Defined in: packages/agent/src/agent/http/index.ts:1343

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/agent/src/agent/http/index.ts:439

boolean


parseTimeFromResponse(response): number

Defined in: packages/agent/src/agent/http/index.ts:1174

Uint8Array

number


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

Defined in: packages/agent/src/agent/http/index.ts:869

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

Options to use to create and send the query.

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/agent/src/agent/http/index.ts:1089

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.

A Canister ID related to this call.

string | Principal

ReadStateOptions

The options for this call.

Identity for the call. If not specified, uses the instance identity.

Identity | Promise<Identity>

any

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

Promise<ReadStateResponse>

Agent.readState


replaceIdentity(identity): void

Defined in: packages/agent/src/agent/http/index.ts:1347

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 @dfinity/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/agent/src/agent/http/index.ts:1284

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/agent/src/agent/http/index.ts:1214

Allows agent to sync its time with the network. Can be called during intialization 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>


static create(options): Promise<HttpAgent>

Defined in: packages/agent/src/agent/http/index.ts:414

HttpAgentOptions = {}

Promise<HttpAgent>


static createSync(options): HttpAgent

Defined in: packages/agent/src/agent/http/index.ts:410

HttpAgentOptions = {}

HttpAgent


static from(agent): Promise<HttpAgent>

Defined in: packages/agent/src/agent/http/index.ts:420

V1HttpAgentInterface | Pick<HttpAgent, "config">

Promise<HttpAgent>