x402 Tutorial: Pay-Per-Call APIs for AI Agents, No API Key Required
Описание и применение
Показано, как использовать протокол x402 для оплаты API по запросу без ключей, включая чтение 402‑ответа, подпись USDC и повторный запрос.
Задача
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