Overview of ICP JavaScript SDK Core
main@icp-sdk/core package.repository·main·Indexed 41 days ago
https://github.com/dfinity/icp-js-coreThe official JavaScript SDK (@icp-sdk/core) for building applications on the Internet Computer. It provides core modules for interacting with the network, including Agent, Candid, Identity, and Principal utilities. Version 5.0.0 includes tools for canister environment configuration and a migration CLI to transition from legacy @dfinity packages.
@icp-sdk/core package.The @icp-sdk/core package is the official JavaScript SDK for the Internet Computer.
packages/core directory of this repository.The ic_env cookie is a URI-encoded string served by the asset canister alongside HTML assets to provide environment variables to the frontend synchronously.
It contains:
ic_root_key: The root of trust for the Internet Computer network where the asset canister is deployed (always present).PUBLIC_ (e.g., PUBLIC_CANISTER_ID:backend).The SDK is composed of several specialized modules for interacting with the Internet Computer:
@icp-sdk/core/agent): Provides the foundation for interacting with the Internet Computer from browsers, Node.js, and other JavaScript runtimes.@icp-sdk/core/candid): A module for working with Candid interfaces.@icp-sdk/core/identity): Manages Identities and enables Web Authentication (WebAuthn) flows.@icp-sdk/core/principal): A module for working with Internet Computer Principals.The Canister Environment allows a frontend application to be configured at runtime without rebuilding or redeploying. This is achieved by the asset canister serving environment variables via a cookie.
The Workflow:
PUBLIC_CANISTER_ID:backend) on the asset canister using the management canister.ic_env cookie.ic_env cookie (which is a URI-encoded string) to retrieve variables like ic_root_key and any variables prefixed with PUBLIC_.rootKey and the specific canisterId) and then makes calls to the backend.@dfinity/assets and @dfinity/auth-client packages are not included in @icp-sdk/core. They will be moved to other @icp-sdk/* packages in the future, but for now, you can continue to use them as standalone packages.The global defaultAgent concept, the getDefaultAgent function, and the ProxyAgent class have been removed from @dfinity/agent.
HttpAgent constructor via HttpAgent.create() or HttpAgent.createSync().HttpAgent class.import { HttpAgent } from '@dfinity/agent';
// Instead of getDefaultAgent()
const agent = await HttpAgent.create();The @icp-sdk/core is a unified package containing all modules required to interact with the Internet Computer. You can install it using your preferred package manager.
npm install @icp-sdk/core
# or
pnpm add @icp-sdk/core
# or
bun add @icp-sdk/core
# or
yarn add @icp-sdk/coreInstall the @icp-sdk/core package using your preferred package manager. This package acts as a meta-package that re-exports functionality from several @dfinity packages for interacting with the Internet Computer.
# Using npm
npm install @icp-sdk/core
# Using yarn
yarn add @icp-sdk/core
# Using pnpm
pnpm add @icp-sdk/coreThe most common way to interact with Internet Computer canisters is by using an Actor. This typically involves using @icp-sdk/bindgen to generate type-safe bindings for your canister interface. You create an HttpAgent with a specific identity and host, then pass that agent into the generated createActor function along with the canister's Principal ID.
import { HttpAgent } from "@icp-sdk/core/agent";
import { Ed25519KeyIdentity } from "@icp-sdk/core/identity";
import { Principal } from "@icp-sdk/core/principal";
import { createActor } from "./api/hello-world";
// Define the canister ID
const canisterId = Principal.fromText('uqqxf-5h777-77774-qaaaa-cai');
const identity = Ed25519KeyIdentity.generate();
// Create the agent
const agent = await HttpAgent.create({
host: 'https://icp-api.io',
identity,
});
// Create the actor using generated bindings
const actor = createActor(canisterId, {
agent,
});
// Call a method on the actor
const response = await actor.greet('world');
console.log(response);tsconfig.json is configured with compatible module resolution settings to correctly handle the @icp-sdk/core submodules.When building a library that depends on @icp-sdk/core, you must specify each submodule you use as an external dependency in your Rollup (or Vite) configuration to ensure they are not bundled into your library.
rollupOptions: {
external: [
'@icp-sdk/core/agent',
'@icp-sdk/core/candid',
'@icp-sdk/core/identity',
'@icp-sdk/core/identity/secp256k1',
'@icp-sdk/core/principal',
],
output: {
globals: {
'@icp-sdk/core/agent': 'icp-sdk-core-agent',
'@icp-sdk/core/candid': 'icp-sdk-core-candid',
'@icp-sdk/core/identity': 'icp-sdk-core-identity',
'@icp-sdk/core/identity/secp256k1': 'icp-sdk-core-identity-secp256k1',
'@icp-sdk/core/principal': 'icp-sdk-core-principal',
},
},
}