x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
Description and use
Implemented a stateless micropayment flow for AI agents using HTTP 402 and EIP‑712 signatures, enabling fine‑grained per‑call pricing.
Problem
AI agents need a low‑overhead way to charge for each API call without adding stateful billing infrastructure.
Outcome
Implemented a stateless, per‑call micropayment flow using HTTP 402 and EIP‑712 signatures, enabling fine‑grained pricing for AI agent endpoints without stateful billing.
Workflow
- Client sends initial request without payment proof.
- Server responds with 402 and a Payload header containing amount, asset, network, payload (nonce), and maxTimestamp.
- Client constructs EIP‑712 typed data, signs it with payer’s private key, and sends a second request with X‑Payment header containing the signature and payer address.
- Server verifies the signature, checks nonce replay protection, and if valid returns 200 OK with the response body; otherwise returns 402 or 400.
- Optional: batch signatures for on‑chain settlement or use a custodial service for off‑chain clearing.
Evidence
x402 flips the model: payment becomes part of the HTTP request/response cycle itself, using a well‑defined status code (402 Payment Required) and a standardized payload format.
Server verifies the signature against the expected typed data. Checks that the nonce hasn’t been used (optional replay protection).
- Record date: 2026-09-15T05:46:57.639Z