快速开始

通过 Agent 接入#

通过 AI 配置 Onchain OS Payment 的收款能力。仅需将提示词发给 AI,即可生成完整的 402 收款接入代码,无需手动实现 402 响应和交易验证。

准备工作#

配置#

将以下提示词发送给 AI,即可生成完整的接入代码,下文中的查询天气 API 仅供参考:

// TS版本
Reference https://github.com/okx/payments/blob/main/typescript/SELLER.md
// Rust版本
Reference https://github.com/okx/payments/blob/main/rust/SELLER.md
// Go版本
Reference https://github.com/okx/payments/blob/main/go/SELLER.md

我有一个查询天气的 API(/weather),帮我部署到localhost:4021,用 onchain-payment-sdk 为它添加收费功能。
每次调用收费 0.1 USDT,网络用 X Layer(eip155:196),收款地址是 0xMyWalletAddress。

运行过程中,AI 会提示配置收款钱包和 API 秘钥。

验证#

通过以下代码请求你的付费资源:

Shell
curl -i http://localhost:4021/weather

请求后,会看到 402 + PAYMENT-REQUIRED 的请求头,说明接入成功:

Http
HTTP/1.1 402 Payment Required // 402状态码
content-type: application/json
payment-required: eyJ4NDAyVmVy.....jAifX1dfQ== // 支付信息(base64 encode编码)

同时会返回 base64 解码后的 payment-required 支付信息:

Json
{
  "x402Version": 2,
  "resource": {
    "url": "/weather",
    "description": "Get current weather data for any location",
    "mimeType": "application/json"
  },
  "accepts": [
    {
      "scheme": "exact",
      "network": "eip155:196",
      "asset": "0x779ded0c9e1022225f8e0630b35a9b54be713736",
      "amount": "1000",
      "payTo": "0xb483abdb92f8061e9a3a082a4aaaa6b88c381308",
      "maxTimeoutSeconds": 600000,
      "extra": {
        "name": "USD₮0",
        "version": "1"
      }
    },
    {
      "scheme": "aggr_deferred",
      "network": "eip155:196",
      "asset": "0x779ded0c9e1022225f8e0630b35a9b54be713736",
      "amount": "1000",
      "payTo": "0xb483abdb92f8061e9a3a082a4aaaa6b88c381308",
      "maxTimeoutSeconds": 600000,
      "extra": {
        "version": "1",
        "name": "USD₮0"
      }
    }
  ]
}