Crate ic_agent

source ·
Expand description

The ic-agent is a simple-to-use library that enables you to build applications and interact with the Internet Computer in Rust. It serves as a Rust-based low-level backend for the DFINITY Canister Software Development Kit (SDK) and the command-line execution environment dfx.


The ic-agent is a Rust crate that can connect directly to the Internet Computer through the Internet Computer protocol (ICP). The key software components of the ICP are broadly referred to as the replica.

The agent is designed to be compatible with multiple versions of the replica API, and to expose both low-level APIs for communicating with Internet Computer protocol components like the replica and to provide higher-level APIs for communicating with software applications deployed as canisters.


The following example illustrates how to use the Agent interface to send a call to an Internet Computer canister that performs network management operations. In this example, the call to the Internet Computer management canister (aaaaa-aa) creates a placeholder for a new canister by registering a network-specific identifier. The management canister then returns the result in the form of the textual representation of the canister identifier to the caller.

use ic_agent::{Agent, export::Principal};
use candid::{Encode, Decode, CandidType, Nat};
use serde::Deserialize;

struct Argument {
  amount: Option<Nat>,

#[derive(CandidType, Deserialize)]
struct CreateCanisterResult {
  canister_id: Principal,

async fn create_a_canister() -> Result<Principal, Box<dyn std::error::Error>> {
  let agent = Agent::builder()
  // Only do the following call when not contacting the IC main net (e.g. a local emulator).
  // This is important as the main net public key is static and a rogue network could return
  // a different key.
  // If you know the root key ahead of time, you can use `agent.set_root_key(root_key);`.
  let management_canister_id = Principal::from_text("aaaaa-aa")?;

  // Create a call to the management canister to create a new canister ID,
  // and wait for a result.
  // The effective canister id must belong to the canister ranges of the subnet at which the canister is created.
  let effective_canister_id = Principal::from_text("rwlgt-iiaaa-aaaaa-aaaaa-cai").unwrap();
  let response = agent.update(&management_canister_id, "provisional_create_canister_with_cycles")
    .with_arg(Encode!(&Argument { amount: None})?)

  let result = Decode!(response.as_slice(), CreateCanisterResult)?;
  let canister_id: Principal = result.canister_id;

let canister_id = create_a_canister().await.unwrap();
eprintln!("{}", canister_id);

For more information about the Agent interface used in this example, see the Agent documentation.


For an introduction to the Internet Computer and the DFINITY Canister SDK, see the following resources:

The Internet Computer protocol and interface specifications are not publicly available yet. When these specifications are made public and generally available, additional details about the versions supported will be available here.






  • Looks up a value in the certificate’s tree at the specified hash.
  • Derive the request ID from a serializable data structure. This does not include the ic-request domain prefix.

Type Aliases§