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.
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:
- Client requests priced resource → Worker checks for 
X-Receiptheader - No receipt? → Generate 
reference, store challenge in KV, return 402 - 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:
| Location | p50 latency | p99 latency | 
|---|---|---|
| San Francisco | 12ms | 35ms | 
| London | 18ms | 42ms | 
| Singapore | 22ms | 48ms | 
| São Paulo | 28ms | 55ms | 
| Sydney | 31ms | 60ms | 
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.