x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
Описание и применение
x402 позволяет автономным ИИ‑агентам оплачивать API‑запросы, используя стандартные HTTP‑заголовки и ERC‑4337 UserOperation, сохраняя безсессионность и простоту интеграции.
Задача
AI agents need to call paid services without manual billing layers, yet traditional payment flows break stateless HTTP contracts.
Результат
Implemented a minimal x402 server protecting an /echo endpoint and a client that pays via ERC‑20 on Base, demonstrating stateless HTTP‑native micropayments for AI agents.
Рабочий процесс
- Send normal GET/POST to resource URL
- Receive 402 Payment Required with Pay‑Payload header
- Extract payment request, sign with wallet to create ERC‑4337 UserOperation
- Base64‑encode UserOperation and add X‑Payment header
- Retry original request with X‑Payment header
- Server verifies payment proof, credits payer, returns 200 OK with resource
Доказательства
The client extracts the payment request, signs it with its wallet, and resends the request with an X‑Payment header containing the signed proof.
Because the payment proof lives in an HTTP header, any HTTP client (fetch, axios, curl, language‑specific HTTP libraries) can participate without changing the endpoint URL or adding a SDK‑specific wrapper.
- Дата записи: 2026-09-14T18:01:01.024Z