monetize-service by coinbase/agentic-wallet-skills
npx skills add https://github.com/coinbase/agentic-wallet-skills --skill monetize-service创建一个使用 x402 支付协议、通过 USDC 收取 API 访问费用的 Express 服务器。调用方在 Base 网络上按请求支付 USDC —— 无需账户、API 密钥或订阅。其他代理可以通过 x402 Bazaar 自动发现您的服务。
x402 是一种原生 HTTP 支付协议。当客户端访问受保护的端点但未支付时,服务器会返回 HTTP 402 状态码并附带支付要求。客户端签署一笔 USDC 支付,并在重试时附带支付头信息。协调者验证并结算支付,然后服务器返回响应。服务在 x402 Bazaar 注册,以便其他代理能够自动发现并为它们付费。
npx awal@2.0.3 status
如果钱包未通过身份验证,请参考 authenticate-wallet 技能。
运行以下命令以获取将接收付款的钱包地址:
npx awal@2.0.3 address
将此地址用作 payTo 的值。
mkdir x402-server && cd x402-server
npm init -y
npm install express @x402/express @x402/core @x402/evm @x402/extensions
创建 index.js:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
const express = require("express");
const { paymentMiddleware } = require("@x402/express");
const { x402ResourceServer, HTTPFacilitatorClient } = require("@x402/core/server");
const { ExactEvmScheme } = require("@x402/evm/exact/server");
const app = express();
app.use(express.json());
const PAY_TO = "<address from step 1>";
// 创建协调者客户端和 x402 资源服务器
const facilitator = new HTTPFacilitatorClient({ url: "https://x402.org/facilitator" });
const server = new x402ResourceServer(facilitator);
server.register("eip155:8453", new ExactEvmScheme());
// x402 支付中间件 —— 保护下方的路由
app.use(
paymentMiddleware(
{
"GET /api/example": {
accepts: {
scheme: "exact",
price: "$0.01",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "此端点返回内容的描述",
mimeType: "application/json",
},
},
server,
),
);
// 受保护的端点
app.get("/api/example", (req, res) => {
res.json({ data: "每次请求花费 $0.01" });
});
app.listen(3000, () => console.log("Server running on port 3000"));
node index.js
使用 curl 进行测试 —— 您应该会收到一个包含支付要求的 402 响应:
curl -i http://localhost:3000/api/example
创建强制执行 x402 支付的 Express 中间件。
| 参数 | 类型 | 描述 |
|---|---|---|
routes | object | 将路由模式映射到支付配置的路由配置对象 |
server | x402ResourceServer | 预配置的 x402 资源服务器实例 |
使用协调者客户端创建。在传递给中间件之前,注册支付方案和扩展。
const { x402ResourceServer, HTTPFacilitatorClient } = require("@x402/core/server");
const { ExactEvmScheme } = require("@x402/evm/exact/server");
const facilitator = new HTTPFacilitatorClient({ url: "https://x402.org" });
const server = new x402ResourceServer(facilitator);
server.register("eip155:8453", new ExactEvmScheme());
| 方法 | 描述 |
|---|---|
register(network, scheme) | 为 CAIP-2 网络标识符注册支付方案 |
路由对象中的每个键是 "METHOD /path" 格式。其值是一个配置对象:
{
"GET /api/data": {
accepts: {
scheme: "exact",
price: "$0.05",
network: "eip155:8453",
payTo: "0x...",
},
description: "端点的人类可读描述",
mimeType: "application/json",
extensions: {
...declareDiscoveryExtension({
output: {
example: { result: "example response" },
schema: {
properties: {
result: { type: "string" },
},
},
},
}),
},
},
}
accepts 字段可以是单个对象或数组(用于多个支付选项):
| 字段 | 类型 | 描述 |
|---|---|---|
scheme | string | 支付方案:"exact" |
price | string | USDC 价格(例如 "$0.01"、"$1.00") |
network | string | CAIP-2 网络标识符(例如 "eip155:8453") |
payTo | string | 接收 USDC 支付的以太坊地址(0x...) |
| 字段 | 类型 | 描述 |
|---|---|---|
accepts | object or array | 支付要求(单个或多个) |
description | string? | 此端点的功能(展示给客户端) |
mimeType | string? | 响应的 MIME 类型 |
extensions | object? | 扩展配置(例如 Bazaar 发现) |
declareDiscoveryExtension 函数将您的端点注册到 x402 Bazaar,以便其他代理可以发现它:
const { declareDiscoveryExtension } = require("@x402/extensions/bazaar");
extensions: {
...declareDiscoveryExtension({
output: {
example: { /* 示例响应体 */ },
schema: {
properties: {
/* 响应格式的 JSON 模式 */
},
},
},
}),
}
| 字段 | 类型 | 描述 |
|---|---|---|
output.example | object | 端点的示例响应体 |
output.schema | object | 描述响应格式的 JSON 模式 |
| 网络 | 描述 |
|---|---|
eip155:8453 | Base 主网(真实 USDC) |
eip155:84532 | Base Sepolia 测试网(测试 USDC) |
app.use(
paymentMiddleware(
{
"GET /api/cheap": {
accepts: {
scheme: "exact",
price: "$0.001",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "廉价数据查询",
},
"GET /api/expensive": {
accepts: {
scheme: "exact",
price: "$1.00",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "高级数据访问",
},
"POST /api/query": {
accepts: {
scheme: "exact",
price: "$0.25",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "运行自定义查询",
},
},
server,
),
);
app.get("/api/cheap", (req, res) => { /* ... */ });
app.get("/api/expensive", (req, res) => { /* ... */ });
app.post("/api/query", (req, res) => { /* ... */ });
app.use(
paymentMiddleware(
{
"GET /api/*": {
accepts: {
scheme: "exact",
price: "$0.05",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "API 访问",
},
},
server,
),
);
app.get("/api/users", (req, res) => { /* ... */ });
app.get("/api/posts", (req, res) => { /* ... */ });
在支付中间件之前注册免费端点:
app.get("/health", (req, res) => res.json({ status: "ok" }));
// 支付中间件仅适用于在其之后注册的路由
app.use(paymentMiddleware({ /* ... */ }, server));
app.get("/api/data", (req, res) => { /* ... */ });
app.use(
paymentMiddleware(
{
"POST /api/analyze": {
accepts: {
scheme: "exact",
price: "$0.10",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "分析文本情感",
mimeType: "application/json",
extensions: {
...declareDiscoveryExtension({
output: {
example: { sentiment: "positive", score: 0.95 },
schema: {
properties: {
sentiment: { type: "string" },
score: { type: "number" },
},
},
},
}),
},
},
},
server,
),
);
app.post("/api/analyze", (req, res) => {
const { text } = req.body;
// ... 您的逻辑
res.json({ sentiment: "positive", score: 0.95 });
});
为同一端点接受多个网络的支付:
"GET /api/data": {
accepts: [
{
scheme: "exact",
price: "$0.01",
network: "eip155:8453",
payTo: EVM_ADDRESS,
},
{
scheme: "exact",
price: "$0.01",
network: "eip155:84532",
payTo: EVM_ADDRESS,
},
],
description: "接受 Base 主网或测试网支付的数据端点",
}
用于生产环境,配合 Coinbase 协调者(支持主网):
npm install @coinbase/x402
const { facilitator } = require("@coinbase/x402");
const { HTTPFacilitatorClient } = require("@x402/core/server");
const facilitatorClient = new HTTPFacilitatorClient(facilitator);
const server = new x402ResourceServer(facilitatorClient);
server.register("eip155:8453", new ExactEvmScheme());
这需要设置 CDP_API_KEY_ID 和 CDP_API_KEY_SECRET 环境变量。请从 https://portal.cdp.coinbase.com 获取。
服务器运行后,使用 pay-for-service 技能测试支付:
# 检查端点的支付要求
npx awal@2.0.3 x402 details http://localhost:3000/api/example
# 发起付费请求
npx awal@2.0.3 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@2.0.3 address 获取钱包地址express、@x402/express、@x402/core、@x402/evm 和 @x402/extensionsx402ResourceServer,并为 eip155:8453 注册 ExactEvmSchemecurl 测试(应收到 402)和 npx awal@2.0.3 x402 pay 测试(应收到 200)每周安装量
1.5K
代码仓库
GitHub 星标数
86
首次出现
2026年2月10日
安全审计
安装于
codex1.3K
opencode1.3K
gemini-cli1.3K
github-copilot1.3K
kimi-cli1.2K
amp1.2K
Create an Express server that charges USDC for API access using the x402 payment protocol. Callers pay per-request in USDC on Base — no accounts, API keys, or subscriptions needed. Your service is automatically discoverable by other agents via the x402 Bazaar.
x402 is an HTTP-native payment protocol. When a client hits a protected endpoint without paying, the server returns HTTP 402 with payment requirements. The client signs a USDC payment and retries with a payment header. The facilitator verifies and settles the payment, and the server returns the response. Services register with the x402 Bazaar so other agents can discover and pay for them automatically.
npx awal@2.0.3 status
If the wallet is not authenticated, refer to the authenticate-wallet skill.
Run this to get the wallet address that will receive payments:
npx awal@2.0.3 address
Use this address as the payTo value.
mkdir x402-server && cd x402-server
npm init -y
npm install express @x402/express @x402/core @x402/evm @x402/extensions
Create index.js:
const express = require("express");
const { paymentMiddleware } = require("@x402/express");
const { x402ResourceServer, HTTPFacilitatorClient } = require("@x402/core/server");
const { ExactEvmScheme } = require("@x402/evm/exact/server");
const app = express();
app.use(express.json());
const PAY_TO = "<address from step 1>";
// Create facilitator client and x402 resource server
const facilitator = new HTTPFacilitatorClient({ url: "https://x402.org/facilitator" });
const server = new x402ResourceServer(facilitator);
server.register("eip155:8453", new ExactEvmScheme());
// x402 payment middleware — protects routes below
app.use(
paymentMiddleware(
{
"GET /api/example": {
accepts: {
scheme: "exact",
price: "$0.01",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "Description of what this endpoint returns",
mimeType: "application/json",
},
},
server,
),
);
// Protected endpoint
app.get("/api/example", (req, res) => {
res.json({ data: "This costs $0.01 per request" });
});
app.listen(3000, () => console.log("Server running on port 3000"));
node index.js
Test with curl — you should get a 402 response with payment requirements:
curl -i http://localhost:3000/api/example
Creates Express middleware that enforces x402 payments.
| Parameter | Type | Description |
|---|---|---|
routes | object | Route config mapping route patterns to payment config |
server | x402ResourceServer | Pre-configured x402 resource server instance |
Created with a facilitator client. Register payment schemes and extensions before passing to middleware.
const { x402ResourceServer, HTTPFacilitatorClient } = require("@x402/core/server");
const { ExactEvmScheme } = require("@x402/evm/exact/server");
const facilitator = new HTTPFacilitatorClient({ url: "https://x402.org" });
const server = new x402ResourceServer(facilitator);
server.register("eip155:8453", new ExactEvmScheme());
| Method | Description |
|---|---|
register(network, scheme) | Register a payment scheme for a CAIP-2 network identifier |
Each key in the routes object is "METHOD /path". The value is a config object:
{
"GET /api/data": {
accepts: {
scheme: "exact",
price: "$0.05",
network: "eip155:8453",
payTo: "0x...",
},
description: "Human-readable description of the endpoint",
mimeType: "application/json",
extensions: {
...declareDiscoveryExtension({
output: {
example: { result: "example response" },
schema: {
properties: {
result: { type: "string" },
},
},
},
}),
},
},
}
The accepts field can be a single object or an array (for multiple payment options):
| Field | Type | Description |
|---|---|---|
scheme | string | Payment scheme: "exact" |
price | string | USDC price (e.g. "$0.01", "$1.00") |
network | string | CAIP-2 network identifier (e.g. "eip155:8453") |
| Field | Type | Description |
|---|---|---|
accepts | object or array | Payment requirements (single or multiple) |
description | string? | What this endpoint does (shown to clients) |
mimeType | string? | MIME type of the response |
extensions | object? | Extensions config (e.g. Bazaar discovery) |
The declareDiscoveryExtension function registers your endpoint with the x402 Bazaar so other agents can discover it:
const { declareDiscoveryExtension } = require("@x402/extensions/bazaar");
extensions: {
...declareDiscoveryExtension({
output: {
example: { /* example response body */ },
schema: {
properties: {
/* JSON schema of the response */
},
},
},
}),
}
| Field | Type | Description |
|---|---|---|
output.example | object | Example response body for the endpoint |
output.schema | object | JSON schema describing the response format |
| Network | Description |
|---|---|
eip155:8453 | Base mainnet (real USDC) |
eip155:84532 | Base Sepolia testnet (test USDC) |
app.use(
paymentMiddleware(
{
"GET /api/cheap": {
accepts: {
scheme: "exact",
price: "$0.001",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "Inexpensive data lookup",
},
"GET /api/expensive": {
accepts: {
scheme: "exact",
price: "$1.00",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "Premium data access",
},
"POST /api/query": {
accepts: {
scheme: "exact",
price: "$0.25",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "Run a custom query",
},
},
server,
),
);
app.get("/api/cheap", (req, res) => { /* ... */ });
app.get("/api/expensive", (req, res) => { /* ... */ });
app.post("/api/query", (req, res) => { /* ... */ });
app.use(
paymentMiddleware(
{
"GET /api/*": {
accepts: {
scheme: "exact",
price: "$0.05",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "API access",
},
},
server,
),
);
app.get("/api/users", (req, res) => { /* ... */ });
app.get("/api/posts", (req, res) => { /* ... */ });
Register free endpoints before the payment middleware:
app.get("/health", (req, res) => res.json({ status: "ok" }));
// Payment middleware only applies to routes registered after it
app.use(paymentMiddleware({ /* ... */ }, server));
app.get("/api/data", (req, res) => { /* ... */ });
app.use(
paymentMiddleware(
{
"POST /api/analyze": {
accepts: {
scheme: "exact",
price: "$0.10",
network: "eip155:8453",
payTo: PAY_TO,
},
description: "Analyze text sentiment",
mimeType: "application/json",
extensions: {
...declareDiscoveryExtension({
output: {
example: { sentiment: "positive", score: 0.95 },
schema: {
properties: {
sentiment: { type: "string" },
score: { type: "number" },
},
},
},
}),
},
},
},
server,
),
);
app.post("/api/analyze", (req, res) => {
const { text } = req.body;
// ... your logic
res.json({ sentiment: "positive", score: 0.95 });
});
Accept payments on multiple networks for the same endpoint:
"GET /api/data": {
accepts: [
{
scheme: "exact",
price: "$0.01",
network: "eip155:8453",
payTo: EVM_ADDRESS,
},
{
scheme: "exact",
price: "$0.01",
network: "eip155:84532",
payTo: EVM_ADDRESS,
},
],
description: "Data endpoint accepting Base mainnet or testnet",
}
For production use with the Coinbase facilitator (supports mainnet):
npm install @coinbase/x402
const { facilitator } = require("@coinbase/x402");
const { HTTPFacilitatorClient } = require("@x402/core/server");
const facilitatorClient = new HTTPFacilitatorClient(facilitator);
const server = new x402ResourceServer(facilitatorClient);
server.register("eip155:8453", new ExactEvmScheme());
This requires CDP_API_KEY_ID and CDP_API_KEY_SECRET environment variables. Get these from https://portal.cdp.coinbase.com.
Once the server is running, use the pay-for-service skill to test payments:
# Check the endpoint's payment requirements
npx awal@2.0.3 x402 details http://localhost:3000/api/example
# Make a paid request
npx awal@2.0.3 x402 pay http://localhost:3000/api/example
| Use Case | Suggested Price |
|---|---|
| Simple data lookup | $0.001 - $0.01 |
| API proxy / enrichment | $0.01 - $0.10 |
| Compute-heavy query | $0.10 - $0.50 |
| AI inference | $0.05 - $1.00 |
npx awal@2.0.3 addressexpress, @x402/express, @x402/core, @x402/evm, and @x402/extensionsx402ResourceServer with facilitator client and register ExactEvmScheme for eip155:8453curl (should get 402) and (should get 200)Weekly Installs
1.5K
Repository
GitHub Stars
86
First Seen
Feb 10, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
codex1.3K
opencode1.3K
gemini-cli1.3K
github-copilot1.3K
kimi-cli1.2K
amp1.2K
97,600 周安装
payTo | string | Ethereum address (0x...) to receive USDC payments |
npx awal@2.0.3 x402 pay