Quickstart

Build with Agent#

Configure payment collection capabilities for Onchain OS Payment through AI. Simply send the prompt to an AI to generate the complete 402 payment integration code -- no need to manually implement the 402 response or transaction verification.

Prerequisites#

Configuration#

Send the following prompt to an AI to generate the complete integration code. The weather query API below is for reference only:

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

I have a weather query API (/weather). Help me deploy it to localhost:4021 and add payment functionality using onchain-payment-sdk.
Charge 0.1 USDT per call, use X Layer network (eip155:196), and the payment address is 0xMyWalletAddress.
Note

During the process, the AI will prompt you to configure your Payment Wallet and API Key.

Verification#

Use the following code to request your paid resource:

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

After the request, you will see a 402 + PAYMENT-REQUIRED response header, indicating that the integration was successful:

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

The base64-decoded payment-required payment information will also be returned:

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"
      }
    }
  ]
}