x402付费API构建器 monetize-service

这是一个区块链支付集成技能,指导开发者如何使用x402协议构建付费API服务。通过Express服务器集成USDC加密货币支付,实现无需账户和API密钥的按请求付费机制。包含支付中间件配置、路由定价、测试方法等完整实现方案,适用于API货币化、微支付、区块链集成等场景。

DApp开发 0 次安装 0 次浏览 更新于 2/24/2026

名称: 货币化服务 描述: 构建并部署一个付费API,其他智能体可以通过x402协议付费使用。当您或用户想要将API货币化、赚钱、盈利、提供服务、向其他智能体销售服务、对端点收费、创建付费端点或设置付费服务时使用。涵盖“通过提供端点赚钱”、“销售服务”、“将数据货币化”、“创建付费API”。 用户可调用: true 禁用模型调用: false 允许的工具: [“Bash(npx awal@latest status*)”, “Bash(npx awal@latest address*)”, “Bash(npx awal@latest x402 details *)”, “Bash(npx awal@latest x402 pay *)”, “Bash(npm *)”, “Bash(node *)”, “Bash(curl *)”, “Bash(mkdir *)”]

构建x402支付服务器

创建一个使用x402支付协议对API访问收取USDC费用的Express服务器。调用方在Base链上按请求支付USDC——无需账户、API密钥或订阅。

工作原理

x402是一种原生HTTP支付协议。当客户端访问受保护的端点但未支付时,服务器返回HTTP 402状态码并附带支付要求。客户端签署USDC支付并携带支付头重试请求。协调器验证并结算支付,服务器返回响应。

确认钱包已初始化并授权

npx awal@latest status

如果钱包未认证,请参考authenticate-wallet技能。

步骤1:获取支付地址

运行以下命令获取接收支付的钱包地址:

npx awal@latest address

将此地址用作payTo值。

步骤2:设置项目

mkdir x402-server && cd x402-server
npm init -y
npm install express x402-express

创建index.js

const express = require("express");
const { paymentMiddleware } = require("x402-express");

const app = express();
app.use(express.json());

const PAY_TO = "<步骤1中的地址>";

// x402支付中间件——保护以下路由
const payment = paymentMiddleware(PAY_TO, {
  "GET /api/example": {
    price: "$0.01",
    network: "base",
    config: {
      description: "此端点返回内容的描述",
    },
  },
});

// 受保护的端点
app.get("/api/example", payment, (req, res) => {
  res.json({ data: "每次请求花费$0.01" });
});

app.listen(3000, () => console.log("服务器运行在端口3000"));

步骤3:运行

node index.js

使用curl测试——您应该会收到带有支付要求的402响应:

curl -i http://localhost:3000/api/example

API参考

paymentMiddleware(payTo, routes, facilitator?)

创建强制执行x402支付的Express中间件。

参数 类型 描述
payTo string 接收USDC支付的以太坊地址(0x…)
routes object 路由配置映射,将路由模式映射到支付配置
facilitator object? 可选的自定义协调器(默认为x402.org

路由配置

路由对象中的每个键都是"METHOD /path"。值可以是价格字符串或配置对象:

// 简单——仅价格
{ "GET /api/data": "$0.05" }

// 完整配置
{
  "POST /api/query": {
    price: "$0.25",
    network: "base",
    config: {
      description: "端点的人类可读描述",
      inputSchema: {
        bodyType: "json",
        bodyFields: {
          query: { type: "string", description: "要运行的查询" },
        },
      },
      outputSchema: {
        type: "object",
        properties: {
          result: { type: "string" },
        },
      },
    },
  },
}

路由配置字段

字段 类型 描述
price string USDC价格(例如"$0.01"、“$1.00”)
network string 区块链网络:“base"或"base-sepolia”
config.description string? 此端点的功能(向客户端显示)
config.inputSchema object? 预期的请求体/查询模式
config.outputSchema object? 响应体模式
config.maxTimeoutSeconds number? 支付结算的最长时间

支持的网络

网络 描述
base Base主网(真实USDC)
base-sepolia Base Sepolia测试网(测试USDC)

模式

多个端点不同价格

const payment = paymentMiddleware(PAY_TO, {
  "GET /api/cheap": { price: "$0.001", network: "base" },
  "GET /api/expensive": { price: "$1.00", network: "base" },
  "POST /api/query": { price: "$0.25", network: "base" },
});

app.get("/api/cheap", payment, (req, res) => { /* ... */ });
app.get("/api/expensive", payment, (req, res) => { /* ... */ });
app.post("/api/query", payment, (req, res) => { /* ... */ });

通配符路由

const payment = paymentMiddleware(PAY_TO, {
  "GET /api/*": { price: "$0.05", network: "base" },
});

app.use(payment);
app.get("/api/users", (req, res) => { /* ... */ });
app.get("/api/posts", (req, res) => { /* ... */ });

健康检查(无需支付)

在支付中间件之前注册免费端点:

app.get("/health", (req, res) => res.json({ status: "ok" }));

// 支付中间件仅适用于之后注册的路由
app.get("/api/data", payment, (req, res) => { /* ... */ });

带请求体模式的POST

const payment = paymentMiddleware(PAY_TO, {
  "POST /api/analyze": {
    price: "$0.10",
    network: "base",
    config: {
      description: "分析文本情感",
      inputSchema: {
        bodyType: "json",
        bodyFields: {
          text: { type: "string", description: "要分析的文本" },
        },
      },
      outputSchema: {
        type: "object",
        properties: {
          sentiment: { type: "string" },
          score: { type: "number" },
        },
      },
    },
  },
});

app.post("/api/analyze", payment, (req, res) => {
  const { text } = req.body;
  // ... 您的逻辑
  res.json({ sentiment: "positive", score: 0.95 });
});

使用CDP协调器(认证)

用于生产环境,配合Coinbase协调器(支持主网):

npm install @coinbase/x402
const { facilitator } = require("@coinbase/x402");

const payment = paymentMiddleware(PAY_TO, routes, facilitator);

这需要CDP_API_KEY_IDCDP_API_KEY_SECRET环境变量。从https://portal.cdp.coinbase.com获取

使用pay-for-service技能测试

服务器运行后,使用pay-for-service技能测试支付:

# 检查端点的支付要求
npx awal@latest x402 details http://localhost:3000/api/example

# 发起付费请求
npx awal@latest x402 pay http://localhost:3000/api/example

定价指南

使用场景 建议价格
简单数据查询 $0.001 - $0.01
API代理/数据增强 $0.01 - $0.10
计算密集型查询 $0.10 - $0.50
AI推理 $0.05 - $1.00

检查清单

  • [ ] 使用npx awal@latest address获取钱包地址
  • [ ] 安装expressx402-express
  • [ ] 定义带价格和描述的路由
  • [ ] 在受保护路由之前注册支付中间件
  • [ ] 将健康/状态端点放在支付中间件之前
  • [ ] 使用curl测试(应收到402)和npx awal@latest x402 pay测试(应收到200)
  • [ ] 宣传您的服务,以便其他智能体发现并使用