Skip to content

Start typing to search the documentation.

Register an agent with the SDK

On this page

The full loop with the TypeScript SDK: register an agent, sign requests as the agent, and verify them in a service (~15 min). Want the 5-minute version first? See Quickstart.

What you need

  • npm install @selfxyz/agent-sdk (Node.js 18+).
  • The Self app with a document that matches the network: a mock passport on testnet, a real passport or ID card on mainnet.

1. Register the agent

requestRegistration starts a session, waitForCompletion polls until the registry mints the agent, and exportKey returns the generated key.

import { requestRegistration } from '@selfxyz/agent-sdk';

const session = await requestRegistration({
  mode: 'linked', // or 'wallet-free' when the human has no wallet
  network: 'testnet', // the SDK defaults to 'mainnet'
  humanAddress: '0xYourWallet',
  disclosures: { minimumAge: 18, ofac: true },
});

// scanUrl is an embeddable QR PNG; deepLink opens the Self app directly
console.log(session.scanUrl);

const { agentId, agentAddress } = await session.waitForCompletion(); // default timeout 30 min
const privateKey = await session.exportKey(); // the agent's identity: store it as a secret
  • Export window: exportKey works from completion until 30 minutes after it, and can be called again inside that window.
  • Disclosures: register with the disclosures your verifiers require. An agent registered with minimumAge: 0 fails .requireAge(18).
  • Ed25519 agents keep their own key: pass ed25519Pubkey and ed25519Signature and skip exportKey (Registration modes).

2. Sign requests as the agent

import { SelfAgent } from '@selfxyz/agent-sdk';

const agent = new SelfAgent({ privateKey: process.env.AGENT_PRIVATE_KEY!, network: 'testnet' });

const res = await agent.fetch('https://some-service.example.com/api/protected', {
  method: 'POST',
  body: JSON.stringify({ hello: 'world' }),
});

3. Verify the agent in a service

The verifier recovers the signer and reads the registry on-chain. No API call, no shared secret.

import express from 'express';
import { SelfAgentVerifier } from '@selfxyz/agent-sdk';

const verifier = SelfAgentVerifier.create()
  .network('testnet')
  .sybilLimit(0) // default 1 agent per human; 0 = no limit
  .build();

const app = express();
app.use(
  express.json({
    verify: (req, _res, buf) => {
      (req as { rawBody?: Buffer }).rawBody = buf; // the signature covers the raw body
    },
  }),
);
app.use('/api', verifier.auth()); // 401 for unverified agents; sets req.agent on success

4. Publish an identity document (optional)

Agents start with a blank agentURI. To make the agent discoverable, host an ERC-8004 registration document and have the NFT owner call setAgentURI(agentId, url). See Agent registration JSON.

Render the QR code without the API

The API is a convenience: the proof goes from the Self app straight to the on-chain Hub, which mints the agent. To keep everything in your own frontend, generate the agent key, sign the registration challenge and build the Self request with @selfxyz/qrcode:

import { SelfAppBuilder, SelfQRcodeWrapper } from '@selfxyz/qrcode';
import { signRegistrationChallenge, buildAdvancedRegisterUserDataAscii } from '@selfxyz/agent-sdk';
import { Wallet } from 'ethers';

const REGISTRY = '0x043DaCac8b0771DD5b444bCC88f2f8BBDBEdd379'; // Celo Sepolia

async function buildLinkedRegistration(humanAddress: string) {
  const agentKey = Wallet.createRandom(); // save agentKey.privateKey: it is the agent's key
  const disclosures = { minimumAge: 18, ofac: true };

  // The agent key proves it controls the key (nonce 0 for a fresh key)
  const sig = await signRegistrationChallenge(agentKey.privateKey, {
    humanIdentifier: humanAddress,
    chainId: 11142220,
    registryAddress: REGISTRY,
    nonce: 0,
  });

  const selfApp = new SelfAppBuilder({
    version: 2,
    appName: 'Self Agent ID',
    scope: 'self-agent-id',
    endpoint: REGISTRY,
    endpointType: 'staging_celo', // 'celo' on mainnet
    userId: humanAddress,
    userIdType: 'hex',
    userDefinedData: buildAdvancedRegisterUserDataAscii({
      agentAddress: agentKey.address,
      signature: sig,
      disclosures,
    }),
    disclosures,
  }).build();

  return { agentKey, selfApp };
}

// <SelfQRcodeWrapper selfApp={selfApp} onSuccess={...} onError={...} />

When the scan succeeds, read the new agent from the registry (getAgentId(zeroPad(agentAddress, 32))) or with new SelfAgent({ privateKey, network }).getInfo(). For a headless flow, getUniversalLink(selfApp) from @selfxyz/qrcode returns the deep link. Without the SDK at all, build the challenge yourself: Implementing without the SDK.

Next

Was this page helpful?