Skip to main content

Client

createClient

The connect2ic client which can be used outside the supported frameworks

Usage

import { createClient } from "@connect2ic/core"
import { InternetIdentity } from "@connect2ic/core/providers/internet-identity"
import { AstroX } from "@connect2ic/core/providers/astrox"
import { PlugWallet } from "@connect2ic/core/providers/plug-wallet"
import * as myCanister from "dfx-generated/myCanister"

const client = createClient({
providers: [
// You may pass in different options to each provider
new InternetIdentity({ providerUrl: "https://ic0.app" }),
new AstroX({ dev: true }),
new PlugWallet(),
],
// Pass in canister definitions (as generated by dfx)
//
// {
// [canisterName: string]: {
// canisterId: string,
// idlFactory: IDL.InterfaceFactory,
// }
// }
canisters: {
myCanister
},

// Global config options for providers
// These are options you want to enable on all providers
globalProviderConfig: {
// Determines whether root key is fetched
// Should be enabled while developing locally & disabled in production
dev: true,
// The host
host: "https://localhost:3000",
// Certain providers require specifying an app name
appName: "my-app",
// Certain providers require specifying which canisters are whitelisted
// Array<string>
whitelist: ["canister-principal-id"],
// Certain providers allow you to specify a canisterId for the Ledger canister
// For example when running it locally
ledgerCanisterId: "ledger-canister-principal-id",
// Certain providers allow you to specify a host for the Ledger canister
// For example when running it locally
ledgerHost: "https://localhost:3000"
},
})

// "connect" | "disconnect" | "init"
client.on("connect", () => {
// Connected
})

// Subscribe to client state changes
const unsub = client.subscribe((state) => {
// New state
state
})

// unsubscribe from state changes
unsub()

// Connect to the specified provider
client.connect(providerId)

// Disconnects the currently active provider
client.disconnect()

// Array<Provider>
client.providers

// Provider
client.activeProvider

// string
client.principal

// { [canisterName: string]: ActorSubclass }
client.actors

// { [canisterName: string]: ActorSubclass }
client.anonymousActors

// "initializing" | "idle" | "connecting" | "connected" | "disconnecting"
client.status

// get / set config
client.config

// Used internally by hooks
client._service