x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
说明与用途
x402 是一种在 HTTP 中的协议级微支付机制,允许 AI 代理请求服务,收到带 Pay 标头的 402 响应,使用 Base 进行支付,然后带 Paid 标头重试以获取资源。
问题
Developers building autonomous AI agents need a lightweight way to charge or pay for fine‑grained services without adding a custom billing layer.
结果
A minimal Express middleware demonstrates how to enforce x402 payments, charging 0.01 USDC per request and validating proof via a Pay/Paid header exchange.
工作流
- Client sends GET /service without payment
- Server detects missing payment and responds 402 with Pay header containing payment URI
- Client executes payment instruction (e.g., sends USDC on Base) and obtains receipt
- Client retries original request with Paid header containing proof URI
- Server validates proof; if valid returns 200 OK with resource
- If proof invalid, server repeats 402 with updated Pay header
证据
The HTTP status code 402 Payment Required was reserved in the original spec but never got a widely adopted implementation.
Below is a self‑contained example that charges 0.01 USDC per call.
- 记录日期: 2026-09-15T06:30:39.892Z