Build and deploy a paid API that other agents can pay to use via x402. Use when you or the user want to monetize an API, make money, earn money, offer a service, sell a service to other agents, charge for endpoints, create a paid endpoint, or set up a paid service. Covers "make money by offering an endpoint", "sell a service", "monetize your data", "create a paid API".
创建一个使用x402支付协议对USDC API访问进行收费的Express服务器。调用者在Base网络上按请求支付USDC——无需账户、API密钥或订阅。
x402是一种基于HTTP的支付协议。当客户端在未支付的情况下访问受保护端点时,服务器返回带有支付要求的HTTP 402状态码。客户端签署一笔USDC支付,并携带支付头信息重试请求。协调者验证并结算支付,服务器返回响应。
bash
npx awal@latest status
如果钱包未认证,请参考authenticate-wallet技能。
运行以下命令获取接收支付的钱包地址:
bash
npx awal@latest address
将此地址用作payTo值。
bash
mkdir x402-server && cd x402-server
npm init -y
npm install express x402-express
创建index.js:
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));
bash
node index.js
使用curl测试——应收到带有支付要求的402响应:
bash
curl -i http://localhost:3000/api/example
创建强制执行x402支付的Express中间件。
| 参数 | 类型 | 描述 |
|---|---|---|
| payTo | string | 接收USDC支付的以太坊地址(0x...) |
| routes |
routes对象中的每个键都是METHOD /path格式。值可以是价格字符串或配置对象:
js
// 简单配置 — 仅价格
{ 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 |
| 网络 | 描述 |
|---|---|
| base | Base主网(真实USDC) |
| base-sepolia |
js
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) => { / ... / });
js
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) => { / ... / });
在支付中间件之前注册免费端点:
js
app.get(/health, (req, res) => res.json({ status: ok }));
// 支付中间件仅适用于在其之后注册的路由
app.get(/api/data, payment, (req, res) => { / ... / });
js
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 });
});
用于生产环境,使用Coinbase协调者(支持主网):
bash
npm install @coinbase/x402
js
const { facilitator } = require(@coinbase/x402);
const payment = paymentMiddleware(PAY_TO, routes, facilitator);
这需要CDPAPIKEYID和CDPAPIKEYSECRET环境变量。从https://portal.cdp.coinbase.com获取这些信息。
服务器运行后,使用pay-for-service技能测试支付:
bash
| 使用场景 | 建议价格 |
|---|---|
| 简单数据查询 | $0.001 - $0.01 |
| API代理/数据增强 |
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 monetize-service-1776420087 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 monetize-service-1776420087 技能
skillhub install monetize-service-1776420087
文件大小: 3.3 KB | 发布时间: 2026-4-17 18:35