发起 x402 支付

使用 Pay 运行 x402 V2 API,并为 Claude Code 提供支持支付的 HTTP 客户端。API、付款方钱包和结算均使用 Solana 沙盒,因此本教程无需账户或真实资金。

你需要准备终端,并安装 Pay 和 Claude Code。

1. 安装 Pay

在 macOS 上,安装预构建的二进制文件:

brew install pay
pay --version

在其他平台上,安装 npm 包:

npm install --global @solana/pay
pay --version

2. 启动 x402 沙盒 API

创建 x402-demo.yaml:

name: x402-demo
subdomain: x402-demo
title: "x402 Demo"
description: "Minimal x402 V2 sandbox endpoint."
category: devtools
version: v1
routing:
type: respond
operator:
currencies:
usd: ["USDC"]
network: "localnet"
fee_payer: true
endpoints:
- method: GET
path: "report"
description: "Return a sandbox report."
metering:
schemes: [x402-exact]
dimensions:
- direction: usage
unit: requests
scale: 1
tiers:
- 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 to
http://127.0.0.1:1402/report.
Before paying, show me the amount and ask for approval. Then return the API
result and payment receipt. Do not make any other paid requests.

该请求遵循一个完整的 x402 V2 支付流程:

  1. API 返回 402 Payment Required,并附带 PAYMENT-REQUIRED 质询。
  2. Pay 验证 x402 要求并显示支付条款。
  3. 批准后,沙盒钱包进行签名,Pay 随后携带 PAYMENT-SIGNATURE 重试该请求。
  4. API 返回结果,并附带 PAYMENT-RESPONSE 收据。

不会动用任何真实资金。拒绝批准将终止该请求。

继续阅读 Solana 上的 x402,了解如何构建 x402 V2 客户端和资源服务器,或者成为 x402 促成者。

Is this page helpful?

Table of Contents

Edit Page
©️ 2026 Solana 基金会版权所有