x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
说明与用途
自治代理可以使用 HTTP 402 和签名头自动支付外部 API 调用,无需手动开票。
问题
AI agents need to pay for external API calls automatically without manual invoicing or custom payment layers.
结果
Agents can autonomously pay for API calls using HTTP 402 and signed headers, eliminating manual invoicing and custom payment logic.
工作流
- 1. Agent sends normal GET/POST to protected endpoint.
- 2. Server responds 402 with Payment header (base64url JSON) and optional WWW‑Authenticate.
- 3. Agent signs the payment request using EIP‑712 and its wallet, adding Authorization: Bearer
<signature>and same Payment header. - 4. Server verifies signature, checks nonce, processes request, returns 200 OK.
证据
The server validates the signature, checks that the nonce hasn’t been reused (prevents replay)
Node.js/Express server protects /data endpoint with x402 payment of 0.01 USDC
- 记录日期: 2026-09-15T01:00:24.396Z