Skip to main content
The Shade Agent API abstracts away the complexity of the TEE and interacting with the agent contract to help you build a Shade Agent quickly.

API overview

The API is packaged as a Docker image and included in your agent when it’s uploaded to Phala Cloud. The API is accessible internally by default on port 3140, but it’s not accessible from outside the TEE. When the API image boots up, it will automatically:
  1. Derive the agent’s NEAR account (a random implicit account)
  2. Fund it with 0.3 NEAR from the NEAR_ACCOUNT_ID specified in the environment variables
  3. Register the agent in the agent contract
The API can be used in any language, but API wrappers are maintained in TypeScript/JavaScript and Python. It’s recommended you develop in TypeScript as it has great synergies with chainsig.js for building multichain transactions.

Setup

npm install @neardefi/shade-agent-js

Agent account ID

Fetches the NEAR account ID of the agent.
import { agentAccountId } from '@neardefi/shade-agent-js';

const res = await agentAccountId();
const accountId = res.accountId;

Agent info

Fetches the code hash and checksum for the agent.
  • The code hash is the code hash of the app image running inside the agent.
  • The checksum is produced by the TEE’s attestation and represents that the agent is registered.
This function will only return the details once the agent has successfully registered in the agent contract. When running the API locally, it will only return the code hash, and not the checksum.
import { agentInfo } from '@neardefi/shade-agent-js';

const res = await agentInfo();
const codehash = res.codehash;
const checksum = res.checksum;

Agent balance

Fetches the NEAR balance of the agent’s account in yoctoNEAR (1 NEAR = 10^24 yoctoNEAR).
import { agent } from '@neardefi/shade-agent-js';

const res = await agent("getBalance");
const balance = res.balance;

Request signature

Requests a signature from the Shade Agent for a multichain account (by calling request_signature on the agent contract).

Parameters

ParameterTypeDescription
pathstringA string that decides which account the signature is for. Different paths derive different accounts.
payloadstringThe hash of the transaction to be signed, given as a hex string.
keyTypestringThe signature scheme: Ecdsa (secp256k1) or Eddsa (ed25519).

Returns

The signature for the transaction.
import { requestSignature } from '@neardefi/shade-agent-js';

const res = await requestSignature({
  path: "ethereum-1",
  payload: "cf80cd8a...",
  keyType: "Ecdsa", // Or "Eddsa"
});
For Ecdsa, the function returns the components of the signature as hex strings. Note that to get r, remove the first two hex characters from big_r.
{
  scheme: 'Secp256k1',
  big_r: {
    affine_point: '03D537AFFD52BE9AF0DA6CF41B573F4BE065434AEE2D25A500BC730C06E7EB2AF1'
  },
  s: {
    scalar: '3470037EB46DC6D1921900B635785290184EC980CFEC7109EB103B5698D4F725'
  },
  recovery_id: 0
}
For Eddsa, the function returns the whole signature as a 64-byte array.
{
  scheme: 'Ed25519',
  signature: [
    5, 105,  30, 208, 192,  39, 154, 105, 252,  20, 132,
    64, 247, 207, 223, 127, 197,  43,  30, 145, 164, 224,
    1,  45, 240,  28, 155, 218, 204,   5, 136, 111, 238,
    40, 120, 122, 249, 166, 193, 174, 120,  94, 177,  39,
    179, 193, 170, 117,  37,  36, 155,  38,  72,  24, 118,
    235, 187, 110, 129,  26, 186,   7,   0,   8
  ]
}
If you’re using the chainsig.js library, you don’t need to worry about the format of these responses since the library handles it.

Agent call

Makes a function call to the agent contract from the agent. This is used for custom contracts when you want to call a function other than request_signature. It returns the result of the function call.
import { agentCall } from '@neardefi/shade-agent-js';

const res = await agentCall({
  methodName: "example_call_method",
  args: {
    arg1: "Value1",
    arg2: "Value2",
  },
  gas: "30000000000000", // Optional
});

Agent view

Makes a function call to a view method (a method that does not require gas) on the agent contract. It returns the result of the function call.
import { agentView } from '@neardefi/shade-agent-js';

const res = await agentView({
  methodName: "example_view_method",
  args: {
    arg1: "value1",
    arg2: "value2",
  },
});

Next steps

Framework Overview

Learn about the core components of the Shade Agent Framework

Security Considerations

Review best practices for secure agent deployment

Build docs developers (and LLMs) love