Skip to content
Cloudflare Docs

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:

Deploy to Cloudflare

Prerequisites

Configuration

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"
}
]
}
}

Selective gating with Bot Management

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.

Deploy

Clone the template, edit wrangler.jsonc, and deploy:

Terminal window
git clone https://github.com/cloudflare/templates
cd templates/x402-proxy-template
npm install
npx wrangler deploy

For full configuration options and Bot Management examples, refer to the template README.

Custom Worker endpoints

For more control, add x402 middleware directly to your Worker using Hono:

TypeScript
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.