Charge for HTTP content
The x402-proxy template is a Cloudflare Worker that sits in front of any HTTP backend. When a request hits a protected route, the proxy returns a 402 response with payment instructions. After the client pays, the proxy verifies the payment and forwards the request to your origin.
Deploy the x402-proxy template to your Cloudflare account:
- A Cloudflare account ↗
- An HTTP backend to gate
- A wallet address to receive payments
Define protected routes in wrangler.jsonc:
{ "vars": { "PAY_TO": "0xYourWalletAddress", "NETWORK": "base-sepolia", "PROTECTED_PATTERNS": [ { "pattern": "/api/premium/*", "price": "$0.10", "description": "Premium API access" } ] }}With Bot Management, the proxy can charge crawlers while keeping the site free for humans:
{ "pattern": "/content/*", "price": "$0.10", "description": "Content access", "bot_score_threshold": 30, "except_detection_ids": [117479730]}Requests with a bot score below bot_score_threshold are directed to the paywall. Use except_detection_ids to allowlist specific crawlers by detection ID.
Clone the template, edit wrangler.jsonc, and deploy:
git clone https://github.com/cloudflare/templatescd templates/x402-proxy-templatenpm installnpx wrangler deployFor full configuration options and Bot Management examples, refer to the template README ↗.
For more control, add x402 middleware directly to your Worker using Hono:
import { Hono } from "hono";import { paymentMiddleware } from "x402-hono";
const app = new Hono<{ Bindings: Env }>();
app.use( paymentMiddleware( "0xYourWalletAddress" as `0x${string}`, { "/premium": { price: "$0.10", network: "base-sepolia", config: { description: "Premium content" }, }, }, { url: "https://x402.org/facilitator" }, ),);
app.get("/premium", (c) => c.json({ message: "Thanks for paying!" }));
export default app;Refer to the x402 Workers example ↗ for a complete implementation.
- Pay Per Crawl — Native Cloudflare monetization without custom code
- Charge for MCP tools — Charge per tool call instead of per request
- x402.org ↗ — Protocol specification