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

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

说明与用途

使用 x402 的自动化配方,实现 HTTP 原生微支付,帮助自主 AI 代理通过链上 ERC‑20 转账自动协商并支付服务费用,无需人工干预。

问题

Autonomous AI agents need to pay for external services (LLM APIs, data feeds, compute) without human intervention.

结果

The recipe demonstrates how to implement an x402 client in Node.js/TypeScript that automatically negotiates and pays for micro‑services via on‑chain ERC‑20 transfers, eliminating manual payment steps for AI agents.

工作流

  • 1. Send initial HTTP request to the service endpoint.
  • 2. If response status is 402, parse JSON body for amount, token, chainId, and nonce.
  • 3. Validate token and chainId against expected values.
  • 4. Sign an ERC‑20 transfer (e.g., USDC on Base) for the specified amount, including the nonce.
  • 5. Send a retry request with X‑Payment and X‑Payment‑Nonce headers containing the transaction hash.
  • 6. Server verifies on‑chain payment and returns 200 with the requested payload.

证据

x402 repurposes 402 as a negotiation signal: client → server → server → client flow

来源

Minimal x402 Client (Node.js / TypeScript) example code

来源

  • 记录日期: 2026-09-15T04:46:09.871Z