x402 Quickstart
Deploy x402-compatible pay-per-call endpoints that accept USDC payments from AI agents on Base.
What is x402?
HTTP 402 ("Payment Required") is the standard for machine-to-machine payments. An agent sends a request, gets a 402 response with pricing info, pays in USDC on Base, and receives the data. No API keys, no accounts, no subscriptions.
Ecosystem (Q1 2026): Stripe, Cloudflare, Google (AP2), Coinbase all ship native x402 support.
Quick Deploy
1. Generate endpoint scaffold
CODEBLOCK0
Creates a ready-to-run Express server with x402 payment middleware at ./my-service/.
2. Configure wallet
Set your Base wallet address to receive payments:
CODEBLOCK1
3. Run locally
CODEBLOCK2
4. Expose via Cloudflare Tunnel (zero cost)
CODEBLOCK3
How It Works
Agent Flow
CODEBLOCK4
Server Flow
- 1. Request arrives without payment → respond
402 with pricing - Request arrives with
X-Payment-Tx header → verify on-chain - If payment confirmed → serve data
- If payment fails → respond
402 again
Endpoint Ideas
High-value endpoints AI agents will pay for:
| Endpoint | Price | Description |
|---|
| INLINECODE5 | $0.50-1.00 | Token/project analysis with scoring |
| INLINECODE6 |
$0.10 | Real-time trading signals |
|
/research/report | $1.00 | Deep research on any topic |
|
/data/portfolio | $0.05 | Multi-chain portfolio snapshot |
|
/tools/scrape | $0.02 | Clean web content extraction |
|
/ai/inference | $0.04 | LLM inference routing |
Payment Verification
The scaffold includes scripts/verify-payment.js for on-chain verification:
CODEBLOCK5
Uses Base RPC (free tier) — no API key needed for basic verification.
Integration with OpenClaw
As a skill endpoint
Add to your agent's capabilities:
CODEBLOCK6
With Cloudflare Tunnel
Zero-cost hosting on your existing machine:
CODEBLOCK7
Spraay Gateway Integration
Register your endpoint on Spraay to get discovered by agents automatically:
CODEBLOCK8
Pricing Strategy
- - Data endpoints: $0.005-0.05 (commodity, high volume)
- Analysis endpoints: $0.10-1.00 (value-add, medium volume)
- Research endpoints: $1.00-5.00 (premium, low volume)
- GPU/compute: $0.05-0.50 (resource-based pricing)
Start low, raise prices when you have traction. Better to have 1000 calls at $0.01 than 0 calls at $1.00.
x402 快速入门
部署兼容 x402 的按调用付费端点,在 Base 网络上接收来自 AI 代理的 USDC 支付。
什么是 x402?
HTTP 402(需要付款)是机器对机器支付的标准。代理发送请求,收到包含定价信息的 402 响应,在 Base 网络上支付 USDC,然后接收数据。无需 API 密钥、无需账户、无需订阅。
生态系统(2026 年第一季度):Stripe、Cloudflare、Google (AP2)、Coinbase 均提供原生 x402 支持。
快速部署
1. 生成端点脚手架
bash
bash scripts/scaffold.sh my-service 我的酷炫 API 0.01
在 ./my-service/ 目录下创建一个包含 x402 支付中间件的、可直接运行的 Express 服务器。
2. 配置钱包
设置您的 Base 钱包地址以接收付款:
bash
在生成的 .env 文件中
WALLET_ADDRESS=0x您的Base钱包地址
USDC_CONTRACT=0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
CHAIN_ID=8453
PORT=3402
3. 本地运行
bash
cd my-service && npm install && node server.js
4. 通过 Cloudflare Tunnel 暴露(零成本)
bash
cloudflared tunnel --url http://localhost:3402
工作原理
代理流程
代理 → GET /api/data → 402 需要付款
├── 价格: 0.01 USDC
├── 支付至: 0x...
└── 网络: base
代理 → 在 Base 上支付 USDC → 获取交易哈希
代理 → GET /api/data (X-Payment-Tx: 0x...) → 200 OK + 数据
服务器流程
- 1. 请求到达但未支付 → 返回包含定价信息的 402 响应
- 请求到达并带有 X-Payment-Tx 头 → 在链上验证
- 如果支付确认 → 提供数据
- 如果支付失败 → 再次返回 402 响应
端点创意
AI 代理愿意付费的高价值端点:
| 端点 | 价格 | 描述 |
|---|
| /alpha/scan | $0.50-1.00 | 带评分的代币/项目分析 |
| /market/signal |
$0.10 | 实时交易信号 |
| /research/report | $1.00 | 任何主题的深度研究 |
| /data/portfolio | $0.05 | 多链投资组合快照 |
| /tools/scrape | $0.02 | 干净的网页内容提取 |
| /ai/inference | $0.04 | LLM 推理路由 |
支付验证
脚手架包含用于链上验证的 scripts/verify-payment.js:
javascript
// 验证 Base 上的 USDC 转账
const verified = await verifyPayment(txHash, expectedAmount, recipientAddress);
使用 Base RPC(免费层)——基本验证无需 API 密钥。
与 OpenClaw 集成
作为技能端点
添加到代理的能力中:
json5
// 在 SKILL.md 或代理配置中
{
tools: [{
name: myx402service,
endpoint: https://my-service.example.com/api,
payment: { currency: USDC, network: base }
}]
}
使用 Cloudflare Tunnel
在现有机器上零成本托管:
bash
一次性设置
cloudflared tunnel create my-x402
cloudflared tunnel route dns my-x402 api.mydomain.com
运行
cloudflared tunnel run --url http://localhost:3402 my-x402
Spraay 网关集成
在 Spraay 上注册您的端点,让代理自动发现:
bash
curl -X POST https://gateway.spraay.app/register \
-H Content-Type: application/json \
-d {url: https://api.mydomain.com, category: data, price: 0.01}
定价策略
- - 数据端点:$0.005-0.05(通用型,高流量)
- 分析端点:$0.10-1.00(增值型,中等流量)
- 研究端点:$1.00-5.00(高端型,低流量)
- GPU/计算:$0.05-0.50(基于资源定价)
从低价开始,获得用户后逐步提价。以 $0.01 的价格获得 1000 次调用,好过以 $1.00 的价格获得 0 次调用。