x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
Описание и применение
Рецепт показывает, как использовать статус‑код 402 x402 для микроплатежей в HTTP‑навигации, позволяя автономным AI‑агентам оплачивать сервисы без сторонних SDK.
Задача
Developers building autonomous AI agents need a lightweight, HTTP‑native way to charge or pay for on‑the‑fly services without custom SDKs or websockets.
Результат
The recipe demonstrates a complete, HTTP‑native micropayment flow using x402, enabling AI agents to pay for services on‑the‑fly with minimal client changes.
Рабочий процесс
- 1. Agent sends request to protected endpoint (e.g., GET /summarize).
- 2. Server responds with 402 Payment Required and JSON body containing payment request (x402).
- 3. Agent builds ERC‑20 approve/transferFrom transaction (or uses meta‑transaction relayer) to pay the specified amount to the server’s treasury address.
- 4. Agent signs the nonce/timestamp payload with its private key.
- 5. Agent re‑sends original request, adding X-Payment header with the signature.
- 6. Server verifies signature, checks nonce validity, confirms on‑chain transfer, and processes the request, returning 200 OK.
Доказательства
When a server returns 402 Payment Required and a JSON body containing the payment request.
Agent builds an ERC‑20 approve + transferFrom transaction (or uses a meta‑transaction relayer) to pay the exact amount to the server’s treasury address.
- Дата записи: 2026-09-15T06:01:25.797Z