Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code)

Описание и применение

Автономные агенты могут платить за API‑запросы, получая 402 Payment Required, подписывая минимальный перевод USDC на Base и повторяя запрос с заголовком X‑Payment, обеспечивая атомарность и беззапасность.

Задача

Autonomous AI agents need to pay for API calls atomically and statelessly without managing long‑lived secrets.

Результат

Implemented a fully working x402 client in Python that can autonomously pay for API calls using USDC on Base, achieving atomic, stateless, and compatible micropayments.

Рабочий процесс

  • 1. Agent sends GET request to protected endpoint without auth.
  • 2. Server responds 402 Payment Required with X‑Payments‑Required header containing JSON payload (amount, token, chain, nonce).
  • 3. Agent parses payload and builds a minimal ERC‑20 transfer transaction to the USDC contract on Base.
  • 4. Agent signs the transaction with its wallet private key and submits it to a Base RPC or relayer, obtaining a tx hash.
  • 5. Agent retries the original request adding X‑Payment header with the tx hash.
  • 6. Server verifies the on‑chain transaction, checks amount and nonce, and if valid returns 200 with the resource. Optionally caches tx hash to prevent replay.

Доказательства

x402 repurposes the HTTP 402 Payment Required status code to let a service request payment before delivering a resource.

Источник

The flow stays within the standard request/response model—no new protocol, just a convention.

Источник

Below is a self‑contained snippet you can drop into an agent that needs to call a hypothetical x402‑protected translation service.

Источник

  • Дата записи: 2026-09-14T19:00:56.827Z