Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
通过 x402 协议实现按次付费的 USDC 微支付。一行代码集成买家 SDK,服务器端 Express 中间件验证支付,无需管理 API 密钥。
pay(url, amount)
@x402/express
@xpaylabs/x402
npm install @xpaylabs/x402
import { pay } from '@xpaylabs/x402'; const response = await pay('https://api.example.com/weather', { amount: '0.001', network: 'base-sepolia', privateKey: '0x...', }); console.log(response.data); // 响应体 console.log(response.paymentId); // 链上交易哈希
import { XPayClient } from '@xpaylabs/x402'; const client = new XPayClient({ privateKey: '0x...', network: 'base-sepolia', facilitatorUrl: 'https://x402-facilitator-testnet.vercel.app/api', }); const res1 = await client.request('https://api.example.com/weather', { amount: '0.001', }); const res2 = await client.request('https://api.example.com/weather', { amount: '0.001', });
import { extractPaymentId } from '@xpaylabs/x402'; const paymentId = extractPaymentId(response.headers); console.log(paymentId); // "0xabc...123"
npm install @x402/express
import express from 'express'; import { x402Middleware } from '@x402/express'; const app = express(); app.use(x402Middleware({ network: 'base-sepolia', facilitatorUrl: 'https://x402-facilitator-testnet.vercel.app/api', evmAddress: '0x...', // 卖家 USDC 收款地址 })); // 付费路由 app.get('/api/generate', (req, res) => { res.json({ result: 'llm response', paymentId: req.x402.paymentId }); }); // 公共路由 app.get('/health', (req, res) => { res.json({ status: 'ok' }); });
AI 代理 (买家) AI API (卖家) │ │ │ 1. POST /api/generate │ │ (未支付) │ │ │ │ 2. 402 Payment Required │ │ { amount: "0.001" } │ │ │ │ 3. 提交 USDC Transfer (链上) │ │ 通过 x402 facilitator │ │ │ │ 4. 重试 + x402 授权头 │ │ │ │ 5. 200 OK + 响应 │ │ (paymentId 在 headers 中) │