Overview
The x402 protocol enables AI agents to pay for API resources using USDC on Base L2 with no login, no API key, and no subscription.@grantex/x402 adds the missing authorization layer — a Grantex Delegation Token (GDT) that proves the paying agent was authorized to spend.
What x402 solves: Machine-to-machine payments.
What Grantex adds: Proof that the payment was authorized — by whom, for what, how much, and when.
Install
Quick Start
1. Issue a Delegation Token
A human principal issues a scoped GDT to their agent:2. Agent Makes Authorized Payments
The agent attaches the GDT to x402 payment requests:3. API Verifies Authorization
The API server requires a valid GDT alongside the x402 payment:How It Works
- Who authorized the spend (principal DID)
- What the agent can access (scoped permissions)
- How much the agent can spend (spend limit + period)
- When the authorization expires
- Chain of delegation for multi-agent scenarios
GDT Token Structure
API Reference
issueGDT(params)
Issue a signed Grantex Delegation Token.
DID of the agent being delegated to.
Array of
resource:action scope strings (e.g., ['weather:read']).{ amount: number, currency: 'USDC' | 'USDT', period: '1h' | '24h' | '7d' | '30d' }ISO 8601 duration (
PT24H, P7D) or shorthand (24h, 7d) or datetime.32-byte Ed25519 private key seed of the issuing principal.
Parent DIDs for sub-delegation chains.
Blockchain for payment authorization.
Promise<string> — The signed GDT JWT.
verifyGDT(token, context)
Verify a GDT against a request context. Checks signature, expiry, revocation, scope, and spend limit.
The GDT JWT to verify.
The
resource:action scope being requested.Spend amount for this request.
'USDC' or 'USDT'.Promise<VerifyResult>:
createX402Agent(config)
Create an x402 fetch wrapper with automatic 402 → pay → retry handling.
x402Middleware(options)
Express middleware for GDT verification.
| Option | Type | Default | Description |
|---|---|---|---|
required | boolean | true | Require GDT (403 if missing) |
requiredScopes | string[] | — | Scopes to enforce |
currency | Currency | 'USDC' | Currency for verification |
extractAmount | (req) => number | — | Custom amount extractor |
Revocation
Instantly revoke a GDT:Audit Log
All GDT operations are logged:Scope Matching
| Granted | Requested | Match |
|---|---|---|
weather:read | weather:read | Yes |
weather:read | weather:write | No |
weather:* | weather:read | Yes |
* | anything:anything | Yes |
CLI
Examples
See the examples directory for runnable demos:- x402-weather-api — Express server with x402 pricing + GDT enforcement
- x402-agent-demo — Agent client that issues a GDT, pays, and fetches data
Security Considerations
- Ed25519 signatures — GDTs are cryptographically signed; tampering invalidates the token
- Scope enforcement — Agents can only access resources explicitly granted
- Spend limits — Per-period spending caps prevent wallet drain
- Instant revocation — Compromised tokens are rejected immediately
- Unique token IDs — Every GDT has a UUID
jtifor replay protection - Audit trail — All issuance, verification, and revocation events are logged
Dependencies
| Package | Purpose |
|---|---|
@noble/ed25519 | Ed25519 key generation and signing |
@noble/hashes | SHA-512 for Ed25519 |
jose | JWT encoding, signing, and verification |