今日已更新 257 条资讯 | 累计 40702 条内容
关于我们

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

Nikhil Ranka 2026年09月08日 17:36 2 次阅读 来源:Dev.to

x402 Explained: HTTP‑Native Micropayments for AI Agents (With Real Code) Target audience: developers building autonomous AI agents who need a lightweight, on‑chain way to charge per‑call without reinventing billing infrastructure. 1. Why x402 matters for agents AI agents frequently invoke other services—LLM endpoints, data feeds, tool wrappers—often dozens or hundreds of times per task. Traditional API‑key or subscription models add operational overhead (key rotation, usage metering, invoicing) and are poorly suited for sub‑cent pricing. x402 is an HTTP status code (402 Payment Required) extension that lets a server signal that a request can be fulfilled only after the client presents a verifiable, on‑chain payment. The flow stays inside the HTTP request/response cycle, so agents can treat a paid call exactly like any other GET/POST: they add a header, retry on 402, and proceed when the header validates. Key properties: Property What it means for agents ** Stateless** No server‑side session needed; each request carries its own proof. ** Atomic** Payment verification and service execution happen in the same request; no separate settlement step. ** Chain‑agnostic** Works with any EVM‑compatible chain that supports ERC‑20 tokens (USDC on Base, Polygon, etc.). ** Minimal overhead** Only a few extra bytes (signature + nonce) added to the request header. 2. The protocol in a nutshell Client sends a normal HTTP request. Server checks for a valid X402-Payment header. If missing or invalid → respond 402 Payment Required with a WWW-Authenticate ‑style challenge that includes: price (amount in smallest token unit) token (ERC‑20 contract address) chainId nonce (server‑generated, prevents replay) Client builds a payment proof: Assemble the message: keccak256(abi.encodePacked(price, token, chainId, nonce, requestBodyHash)) Sign it with an EOA or smart‑wallet private key ( eth_sign ). Encode the signature (v, r, s) and the signer address into the X402-Payment header. Server verifi

本文内容来源于互联网,版权归原作者所有
查看原文