Skip to main content

Client examples

  • Minimal integration
  • Full example (Node + viem)
  • React example (wagmi + viem)
import { wrapFetchWithPayment } from "x402x-fetch";
import { parseEther } from "viem";
const fetchWithPay = wrapFetchWithPayment(fetch, walletClient, parseEther("0.01")); // Set max payment amount
const res = await fetchWithPay("https://api.example.com/premium", { method: "POST" });

Workflow

  1. Call the protected resource → receive 402
  2. Parse accepts → choose one requirements
  3. Generate payment signature (Permit/EIP-3009/Permit2)
  4. Set X-Payment and retry request → receive 200

Best practices

  • Always set maxValue to prevent overpayment
  • For resources with multiple options, pass a selector to prioritize a token
  • Print the payload and 402 body on failures for troubleshooting

FAQs

  • Insufficient balance/expired/nonce error → re-sign as instructed
  • Failures after network switch → ensure network matches the requirements