Detect and pay x402 crypto paywalls automatically. When your agent gets a 402 Payment Required response with x402 JSON, this skill handles payment via Coinbase facilitator on Base network with USDC. Use when agent hits paid APIs, premium content, or any x402-enabled endpoint.
Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
Instruções da origem · Visualização somente leitura
name
x402-agent
description
Detect and pay x402 crypto paywalls automatically. When your agent gets a 402 Payment Required response with x402 JSON, this skill handles payment via Coinbase facilitator on Base network with USDC. Use when agent hits paid APIs, premium content, or any x402-enabled endpoint.
Automatically detect and pay x402 crypto paywalls when your AI agent encounters HTTP 402 Payment Required responses.
When to Activate
Activate this skill when an HTTP response returns status 402 Payment Required and the response body contains an x402Version field with an accepts array describing payment options.
Detection pattern:
HTTP status code: 402
Response body: JSON with x402Version (number) and accepts (array of payment requirements)
import { createAgentFetch } from"@x402-kit/agent";
const agentFetch = createAgentFetch({
walletPrivateKey: process.env.X402_WALLET_PRIVATE_KEYas`0x${string}`,
network: "eip155:8453", // Base mainnetpolicy: {
maxPerRequest: "1.00", // Max 1 USDC per requestmaxDailySpend: "10.00", // Max 10 USDC per dayallowedNetworks: ["eip155:8453"],
allowedAssets: ["0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"], // USDC on BaserequireHumanApproval: false,
},
logFilePath: "./x402-payments.jsonl",
});
Step 3: Use as a Drop-in Fetch Replacement
// Use agentFetch exactly like regular fetchconst response = awaitagentFetch("https://api.example.com/premium/data");
const data = await response.json();
What Happens Internally
The initial request is made normally
If the server returns HTTP 402 with x402 payment requirements, the skill:
Parses the payment requirements from the response headers and body
Finds a matching payment option for the configured network
Converts the raw token amount to human-readable format
Checks the spending policy (per-request limit, daily limit, network/asset/domain rules)
If approved: signs an EIP-3009 payment authorization and retries with the X-PAYMENT header
Records the spend and logs the payment event
If the policy denies the payment, the original 402 response is returned without spending
Configuration
Environment Variables
Variable
Required
Description
X402_WALLET_PRIVATE_KEY
Yes
Hex private key (with 0x prefix) of a wallet funded with USDC on Base
Policy Configuration
The policy object controls what the agent is allowed to spend:
interface X402Policy {
maxPerRequest: string; // Max USDC per single request (e.g., "1.00")maxDailySpend: string; // Max USDC per day (e.g., "10.00")allowedNetworks: string[]; // CAIP-2 networks (e.g., ["eip155:8453"])allowedAssets: string[]; // Token contract addressesdomainAllowlist?: string[]; // Only pay these domains (optional)domainDenylist?: string[]; // Never pay these domains (optional)requireHumanApproval: boolean;
}
const agentFetch = createAgentFetch({
walletPrivateKey: process.env.X402_WALLET_PRIVATE_KEYas`0x${string}`,
network: "eip155:84532", // Base Sepolia testnetpolicy: {
maxPerRequest: "10.00",
maxDailySpend: "100.00",
allowedNetworks: ["eip155:84532"],
allowedAssets: ["0x036CbD53842c5426634e7929541eC2318f3dCF7e"], // USDC on Base SepoliarequireHumanApproval: false,
},
});
Safety Rules
Never expose the private key. Always load X402_WALLET_PRIVATE_KEY from environment variables. Never hardcode it in source files or logs.
Respect spending limits. The policy engine enforces per-request and daily limits. Configure conservative limits for production.
Log all payments. Set logFilePath to maintain an audit trail. The log file records every payment attempt (approved, denied, or failed) in JSONL format.
Use domain filtering. Set domainAllowlist or domainDenylist to restrict which services the agent can pay.
Start with testnet. Use Base Sepolia (eip155:84532) during development. Only switch to mainnet (eip155:8453) for production.
Daily spend resets at UTC midnight. The spend tracker persists to disk and resets automatically each day.
Payment Log Format
Each payment event is logged as a JSON line in the log file: