x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)
Описание и применение
Реализован минимальный сервер x402 на FastAPI, позволяющий автономным агентам автоматически оплачивать и получать ресурсы через заголовки HTTP.
Задача
Autonomous agents need a way to pay for services without human‑in‑the‑loop approvals.
Результат
Implemented a minimal x402 server in FastAPI that protects an endpoint, challenges clients with 402, verifies on‑chain USDC payments, and serves the resource upon successful proof.
Рабочий процесс
- 1. Client sends normal GET request to resource.
- 2. Server responds 402 with Payment-Request header containing price, address, nonce, etc.
- 3. Client decodes header, creates payment proof (tx hash, signature) and sends it in Payment header on retry.
- 4. Server verifies proof against nonce and payment details; if valid returns 200 with payload, else 402.
- 5. Client consumes payload; no further round‑trip needed unless nonce rotated.
Доказательства
The protocol is stateless on the server side beyond the nonce
Below is a self‑contained example that you can drop into a main.py file and run with uvicorn main:app
- Дата записи: 2026-09-14T17:31:32.017Z