跳到正文

HTTP API

查询商户、PaymentRef、receive、receipt、交易和恢复路由。

所有 JSON 路由使用 application/json。商户 API 通过 bearer API key 认证,x402 支付提交使用 PAYMENT-SIGNATURE

核心路由

方法路由用途
GET/healthz网关健康状态。
POST/merchant/challenge创建短期 payTo 所有权挑战。
POST/merchant/register验证签名后注册商户。
GET/merchant/me读取已认证商户资料。
POST/merchant/rotate-key轮换 API 或 notify key。
POST/merchant/revoke-key撤销旧 key。
POST/ref创建支付引用。
GET/ref/:refId读取公开引用元数据。
GET/ref?pay_to=...分页列出已认证 refs。
POST/ref/:refId/revoke撤销引用。
POST/payto/:payTo/revoke退役 payTo。
GET/pay/:refId提供钱包签名 relay 页面。
GET/receive/:payTo协商或提交 x402 支付。
POST/verify-receipt验证收据上下文和链上状态。
POST/verify-refund-receipt验证退款收据与反向转账。
GET/tx/:txHash查询有边界的交易状态。
GET/txs查询有边界的转账历史。
POST/recover-settlement恢复结算后结果,且不再次结算。

认证矩阵

路由组认证方式
Health、challenge、registration、公开 ref公开,但受 route rate limit 约束
Ref mutation/list、payTo、merchant profile 和 key生产环境使用 Authorization: Bearer <apiKey>
Receive 协商与结算付款方提供 x402 PAYMENT-SIGNATURE
Tx、history、receipt、recovery受部署策略和 abuse gate 控制
Order 与 refund mutation商户 bearer key 且启用 order mode

商户注册

POST /merchant/challenge 接收 payTo、network、action: "merchant.register" 和可选 webhook URL。响应 challenge 会绑定 gateway origin、action、payTo、network、webhook hash、nonce 和 expiry。

POST /merchant/register 接收 challenge id、payTo、network、label、webhook URL 和 signature。一次性响应返回 merchantIdapiKeynotifyHmacKeynotifyHmacKeyId 和 payTo。

PaymentRef 路由

POST /ref

JSON
{ "payTo": "0x1111111111111111111111111111111111111111", "network": "base", "asset": "USDC", "amount": "24000000", "invoiceId": "order_7K9M2", "label": "订单 7K9M2", "expiresInSecs": 900, "notifyUrl": "https://merchant.example/x402/notify"}

响应返回 refId、可传输 refUrl 和不可变 payment snapshot。开放金额 ref 省略 amount;invoiceId 必须配合固定 amount。

GET /ref 使用 pay_to、可选有界 limit 和不透明 cursor,返回 items 与可选 nextCursor。不能自行构造或修改 cursor。

Receive 协商

不带 PAYMENT-SIGNATURE 时,/receive/:payTo 返回 402 和 base64url 编码的 payment-required header。带有效支付签名时,它会验证完整上下文、调用 facilitator /verify/settle,并返回包含交易哈希的结算结果。

常见 query 是 ref=<refId>,开放金额还使用 amount=<atomic>;退款路径会绑定 refundOf=sha256:<receiptHash>

PaymentRequired 绑定 resource URL、exact scheme、network、原子金额、token contract、payTo、timeout 与 extension。生产响应应包含 signed offer,并精确绑定 origin、resource、paymentId、payTo、amount、network、chain id、asset、expiry 和 nonce。

结算请求的 PAYMENT-SIGNATURE 是 base64url x402 payload,包含 EIP-3009 signature、authorization 和 payment-identifier。它是可重放敏感数据,不能记录日志。

成功响应返回 x402Versionsuccess、network、transaction hash、payer、receiptStatus、response code/message,以及可选的 offer-receipt 签名和完整 receipt。

验证与历史

POST /verify-receipt

请求包含 signedReceipt、完整 receipt、可选 expectedPayToexpectedAmountminConfirmations。结果会分开返回签名/上下文 validity 与确认数 policy,并提供字段匹配证据。

GET /tx/:txHash

返回 found、status、确认数、block number、转账两端、token contract、value 和可用的有界 logs。

GET /txs

支持 pay_tofromassetfrom_blocklimit 和不透明 cursor。至少需要 pay_tofrom 或 cursor。部署会限制 block span、响应大小、RPC timeout 和调用频率。

POST /recover-settlement

提交 refId、原始 paymentPayload,以及可选 tx hash、signed offer 或 starting block。恢复只检查不可变 ref 上下文和链证据,不能再次调用 facilitator settle。

可选 order 与 refund 路由

方法路由用途
POST/order创建租户隔离 unpaid order 和 ref
GET/order/:orderId读取订单
GET/order按可选 status 列出订单
POST/order/:orderId/cancel取消 unpaid order
POST/order/:orderId/refund创建随机绑定 refund intent
GET/order/:orderId/refund/:refundId读取退款状态
GET/order/:orderId/refunds列出订单退款
POST/order/:orderId/refund/:refundId/reject签发 rejected refund receipt
POST/refund-receipt/rejected签发无状态 rejected refund receipt

所有 order lookup 都按已认证 merchant id 隔离,裸 orderId 不是全局权限。

错误 envelope

JSON
{ "x402Version": 2, "error": { "code": "ref_expired", "message": "payment reference expired", "details": {} }}

稳定 code 包括 invalid_requestunauthorizedforbiddennot_foundref_expiredref_revokedpayto_revokedpayment_requiredpayment_verification_failedsettlement_failedreceipt_invalidrpc_unavailablerate_limited

错误使用稳定的结构化 code,便于 SDK 区分输入错误、策略拒绝、暂时不可用和结算后状态不明确等情况。