x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
Описание и применение
x402 — это протокол‑уровневый способ микроплатежей в HTTP, позволяющий агентам AI запрашивать услуги, получать 402 с заголовком Pay, оплачивать через Base и повторять запрос с заголовком Paid для получения ресурса.
Задача
Developers building autonomous AI agents need a lightweight way to charge or pay for fine‑grained services without adding a custom billing layer.
Результат
A minimal Express middleware demonstrates how to enforce x402 payments, charging 0.01 USDC per request and validating proof via a Pay/Paid header exchange.
Рабочий процесс
- Client sends GET /service without payment
- Server detects missing payment and responds 402 with Pay header containing payment URI
- Client executes payment instruction (e.g., sends USDC on Base) and obtains receipt
- Client retries original request with Paid header containing proof URI
- Server validates proof; if valid returns 200 OK with resource
- If proof invalid, server repeats 402 with updated Pay header
Доказательства
The HTTP status code 402 Payment Required was reserved in the original spec but never got a widely adopted implementation.
Below is a self‑contained example that charges 0.01 USDC per call.
- Дата записи: 2026-09-15T06:30:39.892Z