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.
string
required
DID of the agent being delegated to.
string[]
required
Array of
resource:action scope strings (e.g., ['weather:read']).SpendLimit
required
{ amount: number, currency: 'USDC' | 'USDT', period: '1h' | '24h' | '7d' | '30d' }string
required
ISO 8601 duration (
PT24H, P7D) or shorthand (24h, 7d) or datetime.Uint8Array
required
32-byte Ed25519 private key seed of the issuing principal.
string[]
Parent DIDs for sub-delegation chains.
string
default:"base"
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.
string
required
The GDT JWT to verify.
string
required
The
resource:action scope being requested.number
required
Spend amount for this request.
Currency
required
'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.
x402Middleware requires either requiredAmount or extractAmount. Do not
derive the amount from client-supplied X-Payment-* headers.
Revocation
Instantly revoke a GDT:Audit Log
All GDT operations are logged:Scope Matching
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