How to charge AI agents per API call on Cloudflare Workers with x402
To charge AI agents per API call on Cloudflare Workers, wrap a route with the Agent Tollbooth middleware, set a price in USD and a Base wallet to receive USDC, and test on Base Sepolia for free. The route answers HTTP 402 with the price, the agent retries with a signed payment, and your Worker serves the response once it is verified and settled. To go live on Base mainnet, use your own x402 facilitator or the hosted one.
What HTTP 402 and x402 are, in plain words
HTTP has a status code, 402 Payment Required, that was reserved for paying over the web (RFC 9110). x402 is an open standard that finally puts it to use (x402.org). The exchange has four moves:
- An agent requests your API route with no payment.
- Your server replies 402 with the price, the currency, the network and where to pay.
- The agent retries the same request with a signed USDC payment attached.
- Your server verifies the payment, settles it on the blockchain and then serves the response.
The agent needs no account and no API key with you. You need a wallet to receive the money and a few lines of code in front of the route. For the longer walk-through of the headers, read how an AI tollbooth works, step by step. For the bigger picture of who should charge and who should not, see charging AI agents: pay per crawl, x402 and MCP.
What you need before you start
- A Cloudflare Workers project with at least one route you want to charge for.
- Node.js and npm.
- A wallet address on Base to receive USDC. You do not need a crypto exchange account for this.
Step 1: get a Base wallet address (payTo)
The payTo setting is the address that receives the money. Create a wallet in any self-custody wallet app that supports Base, and copy its public address, which starts with 0x. Never paste a seed phrase or private key into code, a config file or a chat. Only the public address goes in your Worker. Copy the address rather than retyping it: a typo sends real payments to the wrong place.
Step 2: install the middleware
In your Worker project run:
npm install @redcenterstudio/agent-tollbooth-edgeIf you use Claude Code, you can install it as a plugin instead. Add the marketplace, install the plugin, then run the install skill:
/plugin marketplace add Red-Center-Studio/agent-tollbooth-402
/plugin install agent-tollbooth-402@agent-tollbooth-402
/agent-tollbooth-402:install-tollboothStep 3: wrap a Worker route
Wrap the handler you want to charge for. The price is in US dollars, so 0.01 means one cent per call:
import { withTollbooth } from '@redcenterstudio/agent-tollbooth-edge';
export default withTollbooth(
{ priceUsd: 0.01, mode: 'x402', payTo: '0xYourBaseWallet', network: 'eip155:84532' },
async () => Response.json({ ok: true }),
);Replace 0xYourBaseWallet with your own address. Note the network value: eip155:84532 is Base Sepolia, a free test network. Leave it there until the next step passes.
Step 4: test for free on Base Sepolia
Testing costs nothing, because Base Sepolia uses test tokens with no real value. In Claude Code, run /agent-tollbooth-402:test-agent to have a test agent call your route. You are checking three things:
- A request with no payment gets a 402 with the price you set.
- A correctly signed test payment gets the response.
- Your wallet shows the test payment arriving.
You can also see the flow against a live example at tollbooth.redcenterstudio.com/demo/402.
Step 5: go live on Base mainnet
When the test passes, change network to eip155:8453, which is Base mainnet, and deploy. Real payments need a facilitator to verify and settle them, and you have two choices:
- Your own x402 facilitator. This is free. You run it and look after it.
- The hosted facilitator. Buy a Pro Pass ($39 USD once, for one project) or an Agency Pass ($79 USD once, for several projects). Each pass includes up to 10,000 hosted settlements a month. Register a developer account whose payout wallet is the same address as your
payTo.
Either way, payments go straight to your wallet. Red Center Studio takes 0% of payments and never holds the money. The passes and the setup steps are on the Agent Tollbooth pay page.
Pricing tips
- Price per call, and keep it small. x402 suits small, frequent requests. Start with a price that is easy for an agent to accept, then adjust once you see real usage. Our note on how much to charge AI agents goes deeper on per call, per page and per dataset pricing.
- Charge for work, not for being found. Something an agent cannot easily get elsewhere, such as a working tool, fresh data or an answer that costs you compute, is a good candidate.
- Keep health checks and webhook routes free. Uptime monitors and webhooks cannot pay, so wrap only the routes you mean to sell.
- Keep welcome the AI search crawlers that send you visitors. Being cited by AI assistants is how many people find you. Our note on which AI bots do what shows how to tell them apart.
Common mistakes
- Wrong network. Going live while
networkstill sayseip155:84532means you are taking test payments, not real ones. Deployingeip155:8453before you have tested is just as bad. - payTo mismatch. On the hosted facilitator, the payout wallet on your developer account must equal
payToin the Worker. If they differ, settlements will not match. - Testing with real money first. Run the whole flow on Base Sepolia, where a mistake costs nothing.
- A mistyped wallet address. Copy it, do not retype it, and check a small payment arrives before you advertise the route.
- Charging every route. Wrapping the whole Worker puts health checks and webhooks behind the paywall.
Frequently asked questions
Do I need a crypto exchange account?
No. You need a wallet address on Base to receive USDC, and a wallet app can give you one. An exchange account is only needed if you later choose to convert USDC to your local currency.
Is testing free?
Yes. The plugin and testing on Base Sepolia (eip155:84532) cost nothing. You pay only if you choose the hosted facilitator when you go live on Base mainnet.
Does Red Center Studio take a cut of my payments?
No. Red Center Studio takes 0% of payments and never holds the money. Payments go straight to your wallet. The Pro Pass ($39 USD once) and Agency Pass ($79 USD once) are one-time prices for the hosted facilitator, not a share of what you earn.
Do I have to buy a pass to go live?
No. You can run your own x402 facilitator for free. The passes are for people who would rather use the hosted one, with up to 10,000 hosted settlements a month included.
Can I have this set up for me?
Yes. AI Tollbooth Setup is the done-for-you service: crawler policy, paid endpoints and a test-network run before real money moves.
Next steps
Start with Steps 1 to 4 and a one-cent route. When the test passes, pick your facilitator on the Agent Tollbooth pay page. If you want it done for you, see AI Tollbooth Setup.