Charge for HTTP content (2)

Last updated Jun 3, 2026 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"
            }
        ]
    }
}

Note

base-sepolia is a test network. Change to base for production.

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 at or 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:

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:

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.

Was this helpful?

YesNo

On this page

Charge for HTTP content (2) - 图2Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/agents/tools/payments/x402/charge-for-http-content/#page","headline":"Charge for HTTP content","description":"Gate HTTP endpoints with x402 payments using a Cloudflare Worker proxy.","url":"https://developers.cloudflare.com/agents/tools/payments/x402/charge-for-http-content/","inLanguage":"en","image":"https://developers.cloudflare.com/agents/tools/payments/x402/charge-for-http-content/og.png?v=af4a2346e36e5439","dateModified":"2026-06-03","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Charge per tool call in an MCP server using paidTool. title: Charge for MCP tools image: https://developers.cloudflare.com/agents/tools/payments/x402/charge-for-mcp-tools/og.png?v=08d95a07be324c1a


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/agents/llms.txt
Use this file to discover all available pages before exploring further.