x402 Adapter
What is x402?
x402 is Coinbase’s open protocol for HTTP-native payments. When an HTTP server returns a 402 Payment Required response with payment details, an x402-capable client pays and retries. Perfect for AI-to-AI service charging.
Usage
import { probeX402Endpoint, executeAgentPayment } from '@pqsafe/agent-pay'
// Discover payment requirementsconst requirements = await probeX402Endpoint('https://api.aiservice.xyz/analyze')
console.log(requirements.maxAmountRequired) // e.g. 0.001 (USDC)console.log(requirements.network) // 'base-mainnet'
// Create envelope scoped to this endpointconst envelope = createSpendEnvelope({ maxAmount: 1, currency: 'USDC', allowedRails: ['x402'], allowedRecipients: ['api.aiservice.xyz'], validUntil: new Date(Date.now() + 300_000), // 5 min agentId: 'my-agent',})
const signed = createSignedEnvelope(envelope, secretKey)
// Execute — handles 402 dance automaticallyconst result = await executeAgentPayment(signed, { recipient: 'api.aiservice.xyz', amount: 0.001, rail: 'x402',})Environment
X402_FACILITATOR_URL=https://x402.org/facilitatorWALLET_PRIVATE_KEY=0x...BASE_RPC_URL=https://mainnet.base.org