x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
说明与用途
使用 HTTP 402 和 EIP‑712 签名实现了 AI 代理的无状态微支付流程,实现了每次调用的精细计费。
问题
AI agents need a low‑overhead way to charge for each API call without adding stateful billing infrastructure.
结果
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.
工作流
- 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.
证据
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).
- 记录日期: 2026-09-15T05:46:57.639Z