x402-payment
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesex402 Payment Skill
x402 支付 Skill
Invoke x402-enabled AI agent endpoints with automatic token payments on both TRON (TRC20) and EVM-compatible (ERC20) chains.
在TRON(TRC20)和兼容EVM(ERC20)的链上,通过自动代币支付调用支持x402的AI Agent端点。
Overview
概述
The skill enables agents to interact with paid API endpoints. When an agent receives a response, this skill handles the negotiation, signing, and execution of the payment using the tool.
x402-payment402 Payment Requiredx402_invokex402-payment402 Payment Requiredx402_invokePrerequisites
前置条件
- Wallet Configuration (agent-wallet):
- Local mode (recommended): set (required),
AGENT_WALLET_PASSWORD(optional).AGENT_WALLET_DIR - Static mode (env): set exactly one of /
AGENT_WALLET_PRIVATE_KEY.AGENT_WALLET_MNEMONIC - Optional for mnemonic mode: .
AGENT_WALLET_MNEMONIC_ACCOUNT_INDEX - Configure a TRON wallet for TRC20 payments (USDT/USDD) and/or an EVM wallet for ERC20 payments (USDT/USDC).
- Local mode (recommended): set
- TronGrid API Key (optional): is optional. Recommended on Mainnet to reduce rate-limit issues.
TRON_GRID_API_KEY - GasFree (optional): and
GASFREE_API_KEYare only needed when using GasFree-related commands/flows. When configured, the tool will prefer theGASFREE_API_SECRETscheme overexact_gasfree. GasFree also requires an account that is activated with sufficient token balance in the GasFree wallet.exact_permit - Dependencies: Run in the
npm installdirectory before first use.x402-payment/ - ,
TRON_GRID_API_KEY, andGASFREE_API_KEYcan also be set inGASFREE_API_SECRET.x402-config.json
- 钱包配置(agent-wallet):
- 本地模式(推荐): 设置(必填)、
AGENT_WALLET_PASSWORD(可选)。AGENT_WALLET_DIR - 静态模式(环境变量): 必须设置/
AGENT_WALLET_PRIVATE_KEY中的其中一个。AGENT_WALLET_MNEMONIC - 助记词模式可选配置: 。
AGENT_WALLET_MNEMONIC_ACCOUNT_INDEX - 为TRC20支付(USDT/USDD)配置TRON钱包,和/或为ERC20支付(USDT/USDC)配置EVM钱包。
- 本地模式(推荐): 设置
- TronGrid API密钥(可选): 为可选配置。在主网中推荐配置,以减少请求频率限制问题。
TRON_GRID_API_KEY - GasFree(可选): 仅在使用GasFree相关命令/流程时需要和
GASFREE_API_KEY。配置后,工具会优先使用GASFREE_API_SECRET方案而非exact_gasfree。GasFree还要求账户已激活且GasFree钱包中有足够的代币余额。exact_permit - 依赖安装: 首次使用前,在目录下运行
x402-payment/。npm install - 、
TRON_GRID_API_KEY和GASFREE_API_KEY也可在GASFREE_API_SECRET中设置。x402-config.json
Usage Instructions
使用说明
1. Verification
1. 验证
Before making payments, verify your wallet status:
bash
npx tsx x402-payment/src/x402_invoke.ts --check在支付前,先验证钱包状态:
bash
npx tsx x402-payment/src/x402_invoke.ts --check2. Invoking an Agent (v2)
2. 调用Agent(v2版本)
Most modern x402 agents use the v2 "invoke" pattern:
bash
npx tsx x402-payment/src/x402_invoke.ts \
--url https://api.example.com \
--entrypoint chat \
--input '{"prompt": "Your query here"}' \
--network nile大多数现代x402 Agent采用v2版本的"invoke"调用模式:
bash
npx tsx x402-payment/src/x402_invoke.ts \
--url https://api.example.com \
--entrypoint chat \
--input '{"prompt": "Your query here"}' \
--network nile3. Agent Discovery (Direct)
3. Agent 发现(直接方式)
- Manifest: Fetch agent metadata.
bash
npx tsx x402-payment/src/x402_invoke.ts --url https://api.example.com/.well-known/agent.json - List Entrypoints: List available functions.
bash
npx tsx x402-payment/src/x402_invoke.ts --url https://api.example.com/entrypoints
- 清单文件: 获取Agent元数据。
bash
npx tsx x402-payment/src/x402_invoke.ts --url https://api.example.com/.well-known/agent.json - 列出入口点: 查看可用功能。
bash
npx tsx x402-payment/src/x402_invoke.ts --url https://api.example.com/entrypoints
4. GasFree Wallet Info
4. GasFree 钱包信息
Query GasFree wallet information (address, activation status, balance, nonce).
Defaults: network=mainnet, wallet=agent-wallet active TRON wallet.
bash
undefined查询GasFree钱包信息(地址、激活状态、余额、随机数)。
默认配置:网络=主网,钱包=agent-wallet中激活的TRON钱包。
bash
undefinedDefault: mainnet + active TRON wallet
默认:主网 + 激活的TRON钱包
npx tsx x402-payment/src/x402_invoke.ts --gasfree-info
npx tsx x402-payment/src/x402_invoke.ts --gasfree-info
Specify wallet address
指定钱包地址
npx tsx x402-payment/src/x402_invoke.ts --gasfree-info --wallet <YOUR_WALLET_ADDRESS>
npx tsx x402-payment/src/x402_invoke.ts --gasfree-info --wallet <YOUR_WALLET_ADDRESS>
Specify network
指定网络
npx tsx x402-payment/src/x402_invoke.ts --gasfree-info --network nile
npx tsx x402-payment/src/x402_invoke.ts --gasfree-info --network nile
Both
同时指定两者
npx tsx x402-payment/src/x402_invoke.ts --gasfree-info --wallet <YOUR_WALLET_ADDRESS> --network nile
Requires: `GASFREE_API_KEY`, `GASFREE_API_SECRET`. Without `--wallet`, requires a configured TRON wallet from agent-wallet. Returns JSON with `gasFreeAddress`, `active`, `allowSubmit`, `nonce`, and per-token `assets` (balance, fees).npx tsx x402-payment/src/x402_invoke.ts --gasfree-info --wallet <YOUR_WALLET_ADDRESS> --network nile
依赖要求:`GASFREE_API_KEY`、`GASFREE_API_SECRET`。如果未指定`--wallet`,则需要在agent-wallet中配置TRON钱包。返回包含`gasFreeAddress`、`active`、`allowSubmit`、`nonce`以及各代币`assets`(余额、手续费)的JSON数据。5. GasFree Account Activation
5. GasFree 账户激活
Activate a GasFree account that has not been activated yet. Use first to check activation status.
Defaults: network=nile, token=USDT.
--gasfree-infobash
undefined激活尚未激活的GasFree账户。请先使用检查激活状态。
默认配置:网络=nile测试网,代币=USDT。
--gasfree-infobash
undefinedDefault: nile + USDT
默认:nile测试网 + USDT
npx tsx x402-payment/src/x402_invoke.ts --gasfree-activate
npx tsx x402-payment/src/x402_invoke.ts --gasfree-activate
Specify network
指定网络
npx tsx x402-payment/src/x402_invoke.ts --gasfree-activate --network mainnet
npx tsx x402-payment/src/x402_invoke.ts --gasfree-activate --network mainnet
Specify network and token
指定网络和代币
npx tsx x402-payment/src/x402_invoke.ts --gasfree-activate --network nile --token USDT
Requires: TRON wallet configured in agent-wallet, `GASFREE_API_KEY`, `GASFREE_API_SECRET`. Wallet must have enough tokens to cover activation fees (~3.05 USDT on nile). If the account is already activated, returns `{"status": "already_active"}` immediately.
**Activation process:**
1. Queries GasFree account info and checks activation status
2. Transfers `activateFee + transferFee + 1 token` from wallet to gasFreeAddress (on-chain TRC20)
3. Polls for on-chain confirmation (up to 60s)
4. Submits a GasFree signed transaction to transfer tokens back to wallet (triggers activation)
5. Polls until the GasFree transaction completes
Returns JSON with `status`, `depositTxId`, `gasFreeTraceId`, `gasFreeState`, `gasFreeTxHash`, and final `active` status.npx tsx x402-payment/src/x402_invoke.ts --gasfree-activate --network nile --token USDT
依赖要求:agent-wallet中已配置TRON钱包,且拥有`GASFREE_API_KEY`、`GASFREE_API_SECRET`。钱包中必须有足够代币覆盖激活手续费(nile测试网约3.05 USDT)。如果账户已激活,会立即返回`{"status": "already_active"}`。
**激活流程:**
1. 查询GasFree账户信息并检查激活状态
2. 从钱包向gasFreeAddress转账`activateFee + transferFee + 1个代币`(链上TRC20转账)
3. 轮询等待链上确认(最多60秒)
4. 提交GasFree签名交易,将代币转回钱包(触发激活)
5. 轮询直到GasFree交易完成
返回包含`status`、`depositTxId`、`gasFreeTraceId`、`gasFreeState`、`gasFreeTxHash`以及最终`active`状态的JSON数据。6. Cross-Chain Support
6. 跨链支持
- TRON (TRC20): Use (testnet) or
--network nile.mainnet - BSC (ERC20): Use (testnet) or
--network bsc-testnet(mainnet).bsc
- TRON(TRC20): 使用(测试网)或
--network nile(主网)。mainnet - BSC(ERC20): 使用(测试网)或
--network bsc-testnet(主网)。bsc
Supported Networks & Tokens
支持的网络与代币
| Chain | Network Name | Common Tokens | USDT Contract |
|---|---|---|---|
| TRON | | USDT, USDD | |
| TRON | | USDT, USDD | |
| BSC | | USDT, USDC | |
| BSC | | USDT, USDC, DHLU | |
| 链 | 网络名称 | 常用代币 | USDT合约地址 |
|---|---|---|---|
| TRON | | USDT, USDD | |
| TRON | | USDT, USDD | |
| BSC | | USDT, USDC | |
| BSC | | USDT, USDC, DHLU | |
Security Considerations & Rules
安全注意事项与规则
[!CAUTION] Wallet Secret Safety: NEVER output wallet private keys or mnemonic phrases to logs or console. Use agent-wallet managed configuration.
[!CAUTION] 钱包密钥安全: 绝对不要将钱包私钥或助记词输出到日志或控制台。请使用agent-wallet托管配置。
Agent Security Rules:
Agent安全规则:
- No Private Key Output: The Agent MUST NOT print, echo, or output any private key to the dialogue context.
- No Mnemonic Output: The Agent MUST NOT print or expose mnemonic phrases.
- Internal Loading Only: Rely on the tool to load wallet credentials internally via agent-wallet.
- No Export Commands: DO NOT execute shell commands containing the private key as a literal string.
- Silent Environment Checks: Use to verify local mode configuration without leaking secrets.
[[ -n $AGENT_WALLET_PASSWORD ]] && echo "Configured" || echo "Missing" - Use the Check Tool: Use to safely verify addresses.
npx tsx x402-payment/src/x402_invoke.ts --check
- 禁止输出私钥: Agent不得在对话上下文里打印、回显或输出任何私钥。
- 禁止输出助记词: Agent不得打印或泄露助记词。
- 仅内部加载: 依赖工具通过agent-wallet内部加载钱包凭证。
- 禁止导出命令: 不要执行包含明文私钥的shell命令。
- 静默环境检查: 使用来验证本地模式配置,避免泄露敏感信息。
[[ -n $AGENT_WALLET_PASSWORD ]] && echo "Configured" || echo "Missing" - 使用检查工具: 使用安全验证地址。
npx tsx x402-payment/src/x402_invoke.ts --check
Binary and Image Handling
二进制与图片处理
If the endpoint returns an image or binary data:
- The data is saved to a temporary file (e.g., ).
/tmp/x402_image_... - The tool returns JSON with ,
file_path, andcontent_type.bytes - Important: The Agent is responsible for deleting the temporary file after use.
如果端点返回图片或二进制数据:
- 数据会保存到临时文件(例如:)。
/tmp/x402_image_... - 工具会返回包含、
file_path和content_type的JSON数据。bytes - 重要提示: Agent需负责在使用后删除临时文件。
Error Handling
错误处理
Insufficient Allowance
授权额度不足
If allowance is insufficient, the tool will automatically attempt an "infinite approval" transaction. Ensure you have native tokens (TRX or BNB/ETH) for gas.
如果授权额度不足,工具会自动尝试发起"无限授权"交易。请确保钱包中有足够的原生代币(TRX或BNB/ETH)用于支付Gas费。
Insufficient Balance
余额不足
Ensure you have enough USDT/USDC/USDD in your wallet on the specified network.
请确保指定网络的钱包中有足够的USDT/USDC/USDD代币。
Debug Stack Trace
调试堆栈跟踪
Set to include full error stack traces in the JSON output when troubleshooting failures.
X402_DEBUG=1Last Updated: 2026-03-11
设置,在排查故障时可在JSON输出中包含完整的错误堆栈信息。
X402_DEBUG=1最后更新时间: 2026-03-11