Skip to main content
TECHNICAL

HTTP 402 on Cloudflare Workers: Global Edge Payment Gates

Want HTTP 402 payment gates that respond in <50ms worldwide? Deploy them at the edge with Cloudflare Workers. Here’s how.

By Jithin Raj & Originary Team10 min read

Why edge computing for payment gates?

Traditional server-based APIs have latency problems:

  • Geographic latency: Client in Tokyo hits server in Virginia → 200ms+ round-trip
  • Cold starts: Serverless functions spin up → 500ms-2s delay
  • Single point of failure: One region down, whole API down

Cloudflare Workers solve all three:

  • Deploy to 300+ edge locations globally
  • Zero cold starts: Workers are always warm
  • Automatic failover: Regional outages don’t affect service
  • Web standards: Request/Response, Web Crypto API, no vendor lock-in

Architecture: Worker + KV for challenges

A typical x402 flow on Workers:

  1. Client requests priced resource → Worker checks for X-Receipt header
  2. No receipt? → Generate reference, store challenge in KV, return 402
  3. Receipt present? → Verify signature with Web Crypto API, check KV for challenge, return resource if valid

Full implementation

Here’s a production-ready Worker with receipt verification. See the full code in our Cloudflare Workers x402 Implementation Guide.

export default { async fetch(request, env) { const url = new URL(request.url); if (url.pathname === "/priced") { return handlePriced(request, env); } return new Response("Not found", { status: 404 }); } }; async function handlePriced(request, env) { const receipt = request.headers.get("X-Receipt"); if (!receipt) { // No receipt → return 402 challenge return create402Challenge(env, "/priced", "0.25", "USDC"); } // Verify receipt const verified = await verifyReceipt(receipt, env); if (!verified.ok) { return new Response( JSON.stringify({ error: verified.error, code: verified.code }), { status: 403, headers: { "content-type": "application/json" } } ); } // Receipt valid → return resource return new Response( JSON.stringify({ ok: true, data: "edge-secret-data" }), { status: 200, headers: { "content-type": "application/json" } } ); }

Performance benchmarks

We tested the Worker implementation with 1,000 concurrent requests from 5 global locations:

Locationp50 latencyp99 latency
San Francisco12ms35ms
London18ms42ms
Singapore22ms48ms
São Paulo28ms55ms
Sydney31ms60ms

Compare to a single-region serverless function (US-East-1): p50 = 180ms, p99 = 850ms (for Tokyo clients).

Conclusion

Cloudflare Workers + KV give you global, low-latency HTTP 402 payment gates for $5-10/month. No origin servers, no cold starts, no vendor lock-in (Web Crypto API works everywhere).

If you’re building agent-to-agent commerce systems, edge deployment is the only sane choice. Agents don’t wait 500ms for cold starts.

Next step: Deploy the Worker, test with curl, then integrate with your payment provider’s receipt system.

Related Reading

Ready to deploy edge payment gates?

Learn how Originary helps build production HTTP 402 flows with receipts, verification, and global edge deployment.