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
500Raw request body unavailable for signature verification. auth()handles ECDSA and Ed25519 agents (it readsx-self-agent-keytypeandx-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()readsofac[0], which isfalsefor EU ID cards, Aadhaar and KYC documents. See OFAC credentials.
5. Set sybil and provider policy
| Setting | Effect |
|---|---|
.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:
| Check | Action | Expected |
|---|---|---|
| Unsigned | Call the route with no x-self-agent-* headers | 401 Missing agent authentication headers |
| Tampering | Sign body A, send body B with the same headers | 401 Agent not verified on-chain (ECDSA: wrong recovered signer) or Invalid Ed25519 signature |
| Expiry | Send a timestamp older than maxAge (5 min) | 401 Timestamp expired or invalid |
| Replay | Send the identical signed request twice | First accepted, second 401 Replay detected |
Next
Thanks. What went wrong?
Thanks for your feedback!