使用 Pay 运行 x402 V2 API,并为 Claude Code 提供支持支付的 HTTP 客户端。API、付款方钱包和结算均使用 Solana 沙盒,因此本教程无需账户或真实资金。
你需要准备终端,并安装 Pay 和 Claude Code。
1. 安装 Pay
在 macOS 上,安装预构建的二进制文件:
brew install paypay --version
在其他平台上,安装 npm 包:
npm install --global @solana/paypay --version
2. 启动 x402 沙盒 API
创建 x402-demo.yaml:
name: x402-demosubdomain: x402-demotitle: "x402 Demo"description: "Minimal x402 V2 sandbox endpoint."category: devtoolsversion: v1routing:type: respondoperator:currencies:usd: ["USDC"]network: "localnet"fee_payer: trueendpoints:- method: GETpath: "report"description: "Return a sandbox report."metering:schemes: [x402-exact]dimensions:- direction: usageunit: requestsscale: 1tiers:- price_usd: 0.001
启动 API 并保持其运行:
pay --sandbox server start x402-demo.yaml --bind 127.0.0.1:1402
x402-exact 方案使其成为仅支持 x402 的端点。沙盒模式会自动创建并为临时的运营方账户和付款方账户充值。
3. 通过 Pay 启动智能体
在另一个终端中,以沙盒模式启动 Claude Code:
pay --sandbox claude
Pay 会将其本地 MCP 工具附加到智能体会话中。智能体不会获得私钥。
4. 发起一次 x402 请求
将以下提示词粘贴到智能体中:
Use Pay to make exactly one sandbox x402 API request tohttp://127.0.0.1:1402/report.Before paying, show me the amount and ask for approval. Then return the APIresult and payment receipt. Do not make any other paid requests.
该请求遵循一个完整的 x402 V2 支付流程:
- API 返回
402 Payment Required,并附带PAYMENT-REQUIRED质询。 - Pay 验证 x402 要求并显示支付条款。
- 批准后,沙盒钱包进行签名,Pay 随后携带
PAYMENT-SIGNATURE重试该请求。 - API 返回结果,并附带
PAYMENT-RESPONSE收据。
不会动用任何真实资金。拒绝批准将终止该请求。
继续阅读 Solana 上的 x402,了解如何构建 x402 V2 客户端和资源服务器,或者成为 x402 促成者。
Is this page helpful?