x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
说明与用途
使用 HTTP‑402 和 x402 协议为自治 AI 代理实现微支付,支持区块链支付并获取服务。
问题
Developers building autonomous AI agents need a lightweight, standards‑based way to discover, call, and pay for micro‑services without creating custom billing layers.
结果
Implemented a self‑contained Node.js example demonstrating the full x402 flow, enabling stateless, atomic micropayments for AI agent services.
工作流
- Client sends normal HTTP request to micro‑service
- Server responds 402 with Pay header containing payment pointer and metadata
- Client creates blockchain transaction to pay the specified amount
- Client retries request with X‑Payment header containing transaction proof
- Server validates proof and returns 200 with payload or repeats 402 if invalid
证据
The server responds 402 with a header (Pay) that tells the client how and how much to pay.
Below is a self‑contained example that shows both sides of the flow.
- 记录日期: 2026-09-14T06:00:37.855Z