x402 Tutorial: Pay-Per-Call APIs for AI Agents, No API Key Required
说明与用途
演示如何使用 x402 通过支付 USDC 以按调用计费的方式访问 API,无需 API 密钥。
问题
Agents need to use third‑party APIs without pre‑creating accounts or storing API keys, yet still pay per request.
结果
Demonstrated how to inspect a 402 challenge, interpret payment terms, and pay with USDC to access services at $0.005 per call.
工作流
- Send normal request to endpoint
- Receive 402 with PAYMENT-REQUIRED header
- Decode base64 JSON to get payment options
- Choose option and sign USDC transaction
- Retry request with PAYMENT-SIGNATURE header
- Server verifies payment and returns response with PAYMENT-RESPONSE header
证据
The server answers an unpaid request with HTTP 402 Payment Required and a machine‑readable price.
Running it prints something like: exact on eip155:8453: $0.005 to 0xF732ea490c5766071785a2310523f7fA2CEbB829 MPP offered: True
- 记录日期: 2026-10-05T15:40:53.341Z