{"slug": "i-let-ai-agents-pay-my-api-per-call-in-usdc-x402-on-cloudflare-s-free-plan-zero", "title": "I let AI agents pay my API per call in USDC (x402 on Cloudflare's free plan, zero dependencies)", "summary": "A developer built and deployed a working pay-per-call API on Cloudflare Pages' free plan that uses the x402 protocol to charge AI agents $0.001–$0.005 in USDC on Base per request, with no npm dependencies, API keys or signup. The service returns an HTTP 402 with machine-readable payment terms, verifies the agent's signed authorization through public facilitators PayAI (with xpay as fallback), runs the handler, and settles only on success so failed calls cost the agent nothing. The developer also validated the flow end-to-end with an empty wallet, confirming the facilitator rejected it solely for insufficient balance.", "body_md": "*Disclosure: this post was written with AI assistance and reviewed before publishing. I sell a starter kit based on this code (link at the end); everything below works without buying it.*\n\nHTTP has had a status code for this since 1997: **402 Payment Required**. With the x402 protocol it finally does something. An AI agent calls your endpoint, gets a 402 with machine-readable payment terms, signs a USDC authorization and retries. Then it gets the data. There's no signup, no API key and no Stripe account.\n\nI wanted to know how little it takes, so I shipped a real one: [freetier-agent-api.pages.dev](https://freetier-agent-api.pages.dev). It's one `_worker.js` on Cloudflare Pages (free plan) with **no npm dependencies**.\n\n`GET /api/apis/search?q=weather` with no payment returns `PAYMENT-REQUIRED` header (x402 v2) and a JSON body (x402 v1, for older clients). Both say: pay 5000 atomic units of USDC (= $0.005) on Base to this address.`transferWithAuthorization` and retries with a `PAYMENT-SIGNATURE` header.`/verify`, runs the handler, then calls `/settle`. The facilitator broadcasts the transfer and pays the gas, and the USDC lands in my wallet.\nCoinbase's facilitator needs CDP keys on mainnet. Several public facilitators don't: PayAI (`facilitator.payai.network`) and xpay (` facilitator.xpay.sh`) both settle Base mainnet USDC without auth. I use PayAI and fall back to xpay:\n\n``` js\nconst FACILITATORS = ['https://facilitator.payai.network', 'https://facilitator.xpay.sh'];\nasync function fac(op, body) {\n  for (const f of FACILITATORS) {\n    try {\n      const r = await fetch(`${f}/${op}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });\n      const j = await r.json();\n      if ('isValid' in j || 'success' in j) return j;\n    } catch {}\n  }\n  return { error: 'no facilitator' };\n}\n```\n\nVerify, run, settle, in that order. If the handler fails (bad URL, missing parameter), return 400 and **never call settle**, so the agent pays nothing. This matters, because agents retry a lot.\n\nI generated a brand-new empty wallet with `viem`, signed a correct authorization and sent it to the live API. The facilitator answered `invalid_exact_evm_insufficient_balance`. So the signature, EIP-712 domain (` USD Coin`, version `2`, chain 8453), amount and recipient were all accepted, and the only thing missing was money. That's a nice zero-cost end-to-end check.\n\nA paid API nobody can find earns nothing. What worked:\n\n`extensions.bazaar` with input/output JSON schemas)`/openapi.json` with `x-payment-info` on each paid operation, plus `/.well-known/x402`\n`POST https://api.x402dash.com/v1/register {\"url\": ...}` per endpoint. It probes the 402 live`@x402/extensions` handles`/api/apis/search` ($0.001): 2,230 link-checked public and free-tier APIs, with filters like `auth=none&alive=yes`\n`/api/free-tiers` ($0.001): current free-tier limits for dev services and LLM APIs`/api/robots-check` ($0.001): may user-agent X crawl this URL?`/api/page-meta` ($0.001): title/OG/headings of a public page (respects robots.txt)\nFree: `/api/apis/sample`, `/openapi.json`, `/health`.\n\nIf you want the cleaned-up code with tests and the listing steps, it's in the [x402 Pay-Per-Call Starter](https://freetier-kits-shop.fourthwall.com/products/x402-pay-per-call-starter). Or just read the 402 response from the live API and build your own.", "url": "https://wpnews.pro/news/i-let-ai-agents-pay-my-api-per-call-in-usdc-x402-on-cloudflare-s-free-plan-zero", "canonical_source": "https://dev.to/freetierkits/i-let-ai-agents-pay-my-api-per-call-in-usdc-x402-on-cloudflares-free-plan-zero-dependencies-28d2", "published_at": "2026-10-19 09:07:09+00:00", "updated_at": "2026-10-19 09:14:42.608899+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "ai-tools", "ai-infrastructure", "developer-tools"], "entities": ["Cloudflare", "Coinbase", "PayAI", "xpay", "Base", "USDC", "x402"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/i-let-ai-agents-pay-my-api-per-call-in-usdc-x402-on-cloudflare-s-free-plan-zero", "markdown": "https://wpnews.pro/news/i-let-ai-agents-pay-my-api-per-call-in-usdc-x402-on-cloudflare-s-free-plan-zero.md", "text": "https://wpnews.pro/news/i-let-ai-agents-pay-my-api-per-call-in-usdc-x402-on-cloudflare-s-free-plan-zero.txt", "jsonld": "https://wpnews.pro/news/i-let-ai-agents-pay-my-api-per-call-in-usdc-x402-on-cloudflare-s-free-plan-zero.jsonld"}}