Skip to content

Start typing to search the documentation.

Verifying agents in a service

On this page

Add Self Agent ID verification to an Express API (~10 min). Afterwards, only registered, human-backed agents with a fresh proof reach your protected routes. Verification reads the registry on-chain, with no API key and no call to Self.

1. Install the SDK

npm install @selfxyz/agent-sdk express

Needs Node 18+. Not on Node.js? Implement the check yourself from the signing scheme.

2. Create a verifier

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

const verifier = SelfAgentVerifier.create()
  .network('mainnet') // default; use 'testnet' for agents registered on Celo Sepolia
  .sybilLimit(0) // no cap; the default (1) rejects humans with several agents
  .rateLimit({ perMinute: 10 })
  .build();

Start without credential checks, confirm a signed request passes, then add policy (step 4). All builder methods and defaults: Verifier options.

3. Add the middleware

The signature covers the exact request body bytes, so keep the raw body:

import express from 'express';

const app = express();
app.use(
  express.json({
    verify: (req, _res, buf) => {
      (req as { rawBody?: Buffer }).rawBody = buf;
    },
  }),
);

app.use('/api', verifier.auth());

app.post('/api/data', (req, res) => {
  const agent = req.agent!; // VerifiedAgent
  res.json({ agentId: agent.agentId.toString(), address: agent.address });
});
  • Don’t mutate or re-serialize the body before verifying. A changed body makes an ECDSA signature recover a different address, which shows up as Agent not verified on-chain.
  • A request with a body but no raw body gets 500 Raw request body unavailable for signature verification.
  • auth() handles ECDSA and Ed25519 agents (it reads x-self-agent-keytype and x-self-agent-key).
  • Error strings and the fields of req.agent: Responses from auth(). Without Express: Without Express.

4. Restrict by credentials

const restricted = SelfAgentVerifier.create()
  .requireAge(21)
  .requireOFAC()
  .requireNationality('USA', 'GBR', 'DEU')
  .sybilLimit(0)
  .includeCredentials()
  .build();

app.post('/api/restricted', restricted.auth(), (req, res) => {
  const { nationality, olderThan } = req.agent!.credentials!;
  res.json({ nationality, olderThan: olderThan.toString() });
});
  • An agent passes only if its human disclosed those credentials at registration.
  • The testnet demo agents (1, 5, 72) proved no age, so requireAge(18) rejects them: Agent's human does not meet minimum age (required: 18, got: 0). Test with an agent registered with the same disclosures.
  • requireOFAC() reads ofac[0], which is false for EU ID cards, Aadhaar and KYC documents. See OFAC credentials.

5. Set sybil and provider policy

SettingEffect
.sybilLimit(1) (default)One agent per human. A human with 13 agents fails with Human has 13 agents (max 1)
.sybilLimit(n)Up to n agents per human
.sybilLimit(0)No cap: group by req.agent.nullifier instead
.requireSelfProvider() (on)Proof must come from Self’s provider. Turn it off only to accept other approved providers

The registry has its own cap: 10 agents per human on Celo Mainnet, none on Celo Sepolia.

6. Test before going live

Run these checks against a registered agent:

CheckActionExpected
UnsignedCall the route with no x-self-agent-* headers401 Missing agent authentication headers
TamperingSign body A, send body B with the same headers401 Agent not verified on-chain (ECDSA: wrong recovered signer) or Invalid Ed25519 signature
ExpirySend a timestamp older than maxAge (5 min)401 Timestamp expired or invalid
ReplaySend the identical signed request twiceFirst accepted, second 401 Replay detected

Next

Was this page helpful?