什么是 x402。
x402 是一个开放协议,用于 HTTP 原生支付,由 Coinbase 于 2024 年底发布。它定义了服务器如何返回 402 Payment Required 以及结构化的 JSON 主体 - 一系列可接受的支付要求(方案、网络、链 ID、接收地址、USDC 基本单位的金额和支付请求 ID) - 以便机器客户端可以解析响应、链上支付,并使用 X-Payment 头重试原始调用,全部以编程方式进行。
HTTP 402闲置了三十年。x402使其发挥作用。
The 402 Payment Required status code was reserved in the original HTTP spec for "future use" and never received a standardized implementation. For three decades, every actual payment flow on the web was built on top of 200 / 401 / 403: a server returns 401 when authentication is missing, the client signs up via a separate UI flow, gets credentials, and re-authenticates. The signup happens out of band. None of this works for AI agents, which cannot complete signup forms.
x402 fixes this by giving 402 a usable specification. A server can return 402 with structured machine-readable payment instructions; a client can parse those instructions, pay, and retry, all without ever touching a signup form. The protocol is the missing piece between "AI agents can call APIs" and "AI agents can pay for the APIs they call". Before x402, the only practical agent payment paths were pre-paid balance accounts (with manual top-up) or proprietary integrations per API. Both broke at scale.
x402的战略重要性在于它为生态系统的其余部分建立了一个标准。钱包实现一次x402处理程序,所有x402兼容的API都可以与该钱包一起使用。服务器框架实现一次x402中间件,所有x402感知的客户端都可以支付它们。促进者(结算基础支付的服务)在服务质量上竞争,而不是在锁定上竞争。
一次响应,一次重试,一次付款。
生命周期有三个状态:未支付调用、支付、重试调用。以下是未支付调用响应在网络上的实际样子。
HTTP/1.1 402 Payment Required Content-Type: application/json { "resource": "POST /api/research-query", "accepts": [ { "scheme": "exact-usdc", "network": "mainnet", "chainId": "eip155:8453", "payToAddress": "0xAgent...", "amountWeiUsdc": "50000", "paymentRequestId": "pr_01J9...", "maxAgeSeconds": 60 } ] }
- Status code 402. Distinct from 401 (auth missing) and 403 (auth refused). The presence of 402 signals "payment is the missing input."
- resource + accepts[]. 主体命名受保护的资源并列出一个或多个可接受的支付要求。迁移可以引用两个要求(例如主网和测试网),并让客户端选择。
- scheme / network / chainId. 支付方案(exact-usdc)、链(主网是 Base)及其 CAIP-2 ID。客户端在付款前根据这些进行匹配。
- payToAddress / amountWeiUsdc. 收款钱包和以 USDC 基本单位表示的确切金额(50000 = 0.05 USDC,6 位小数)。客户端在付款前验证这两者。
- paymentRequestId + maxAgeSeconds. 该 ID 将付款与此报价关联;maxAgeSeconds 是付款必须多新。引用不同 ID 的付款或超过窗口的付款将被拒绝。
- 重试携带 X-Payment 头部。 客户在链上付款,然后重新发出原始请求,附带证明付款的 X-Payment 头部。服务器验证并返回实际结果。没有响应头或托管页面是 x402 线路的一部分。
x402在实际应用中。
三个具体的部署,按每层的采用成熟度排序。
MCP服务器向未支付的工具调用返回402挑战
一个付费的 MCP 工具接收到来自 Claude Desktop 的调用,而调用者尚未标记为已付款。服务器构建一个 requirePayment 挑战 - 一个 402,其主体携带价格和 hostedUrl - 并将其作为工具错误返回。Claude Desktop 在聊天中渲染链接;用户支付 0.05 美元 USDC;对同一工具的下一次调用成功。
API 端点按调用计费
一个研究 API 在 x402 服务器适配器后每次调用收费 0.50 美元。AI 代理调用端点,获得 402 列出金额和收款人,从其钱包在 Base 上付款,并重新发出带有 X-Payment 头部的请求。适配器验证头部并返回已付款响应。整个循环在 60 秒内完成。
代理通过 x402 识别的钱包以编程方式付款
代理的运行时是x402-aware(它实现了规范)。当它遇到402时,它从accepts[]中读取payToAddress和金额,从其预授权钱包(在其支出权限范围内)签署转账,在Base上广播,并使用X-Payment头重试原始调用。没有人工干预;循环在5-10秒内完成。
在堆栈中围绕 x402 的内容。
x402是协议。这三个术语涵盖了它所依赖的概念和它所支持的用例。