技能标识:x402-agent
Detect and pay x402 crypto paywalls automatically. When your agent
当您的 AI 代理遇到 HTTP 402 Payment Required 响应时,自动检测并支付 x402 加密付费墙。
当 HTTP 响应返回 状态码 402 Payment Required 且响应体包含带有描述支付选项的 accepts 数组的 x402Version 字段时,激活此技能。
检测模式:
bash
npm install @x402-kit/agent @x402-kit/shared
typescript
import { createAgentFetch } from @x402-kit/agent;
const agentFetch = createAgentFetch({
walletPrivateKey: process.env.X402WALLETPRIVATE_KEY as 0x${string},
network: eip155:8453, // Base 主网
policy: {
maxPerRequest: 1.00, // 每次请求最多 1 USDC
maxDailySpend: 10.00, // 每天最多 10 USDC
allowedNetworks: [eip155:8453],
allowedAssets: [0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913], // Base 上的 USDC
requireHumanApproval: false,
},
logFilePath: ./x402-payments.jsonl,
});
typescript
// 像使用普通 fetch 一样使用 agentFetch
const response = await agentFetch(https://api.example.com/premium/data);
const data = await response.json();
| 变量 | 必需 | 描述 |
|---|---|---|
| X402WALLETPRIVATE_KEY | 是 | 在 Base 上持有 USDC 的钱包的十六进制私钥(带 0x 前缀) |
策略对象控制代理允许支出的内容:
typescript
interface X402Policy {
maxPerRequest: string; // 单次请求最大 USDC(例如 1.00)
maxDailySpend: string; // 每天最大 USDC(例如 10.00)
allowedNetworks: string[]; // CAIP-2 网络(例如 [eip155:8453])
allowedAssets: string[]; // 代币合约地址
domainAllowlist?: string[]; // 仅支付这些域名(可选)
domainDenylist?: string[]; // 绝不支付这些域名(可选)
requireHumanApproval: boolean;
}
| 网络 | CAIP-2 ID | USDC 地址 |
|---|---|---|
| Base(主网) | eip155:8453 | 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 |
| Base Sepolia(测试网) |
typescript
interface AgentFetchConfig {
walletPrivateKey: Hex; // 来自环境变量:X402WALLETPRIVATE_KEY
policy: X402Policy; // 支出规则
network: Network; // CAIP-2 格式(例如 eip155:8453)
spendFilePath?: string; // 每日支出持久化(默认:.x402/daily-spend.json)
logFilePath?: string; // 支付日志文件路径
rpcUrl?: string; // 自定义 JSON-RPC URL
tokenDecimals?: number; // 代币小数位数(USDC 默认:6)
}
代理调用 api.example.com/premium,收到 402,支付 0.10 USDC,获取数据:
typescript
const agentFetch = createAgentFetch({
walletPrivateKey: process.env.X402WALLETPRIVATE_KEY as 0x${string},
network: eip155:8453,
policy: {
maxPerRequest: 0.50,
maxDailySpend: 5.00,
allowedNetworks: [eip155:8453],
allowedAssets: [0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913],
requireHumanApproval: false,
},
});
const response = await agentFetch(https://api.example.com/premium/weather);
// 第一次请求返回 402 -> 代理自动支付 0.10 USDC -> 重试 -> 获取 200 和数据
typescript
const agentFetch = createAgentFetch({
walletPrivateKey: process.env.X402WALLETPRIVATE_KEY as 0x${string},
network: eip155:8453,
policy: {
maxPerRequest: 1.00,
maxDailySpend: 20.00,
allowedNetworks: [eip155:8453],
allowedAssets: [0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913],
domainAllowlist: [api.trusted-service.com, data.partner.io],
requireHumanApproval: false,
},
});
typescript
const agentFetch = createAgentFetch({
walletPrivateKey: process.env.X402WALLETPRIVATE_KEY as 0x${string},
network: eip155:84532, // Base Sepolia 测试网
policy: {
maxPerRequest: 10.00,
maxDailySpend: 100.00,
allowedNetworks: [eip155:84532],
allowedAssets: [0x036CbD53842c5426634e7929541eC2318f3dCF7e], // Base Sepolia 上的 USDC
requireHumanApproval: false,
},
});
每个支付事件作为 JSON 行记录在日志文件中:
json
{
timestamp: 2026-02-27T12:00:00.000Z,
url: https://api.example.com/premium/data,
amount: 0.1,
asset: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913,
network: eip155:8453,
facilitator: ,
success: true,
policyDecision: approved
}
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 x402-paywall-kit-1776099321 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 x402-paywall-kit-1776099321 技能
skillhub install x402-paywall-kit-1776099321
文件大小: 145.08 KB | 发布时间: 2026-4-15 15:02