HTTP 402 Agentic Payments: x402 Protocol, MPP, and Monetization
How HTTP 402 agentic payments work with x402 and Machine Payments Protocol on Workers, MCP tools, gateways, and edge monetization.
This page summarizes Cloudflare’s documentation on agentic payments—programmatic pay-per-request flows built on HTTP 402 Payment Required—together with related x402 and Machine Payments Protocol (MPP) guidance, plus AWS WAF’s Monetize action for bot traffic. The dominant pattern is the same everywhere: challenge, pay, retry with proof, then receive the resource and a receipt.
HTTP 402 agentic payments: why agents pay without accounts
Section titled “HTTP 402 agentic payments: why agents pay without accounts”Traditional API access often forces account signup, stored payment methods, and long-lived API keys before anything can be called. Agentic payments let clients (including AI agents) discover a price, pay, and consume the resource in the same HTTP conversation.
AI agents need to discover, pay for, and consume resources and services programmatically. Traditional onboarding requires account creation, a payment method, and an API key before an agent can pay for a service. Agentic payments let AI agents purchase resources and services directly through the HTTP 402 Payment Required response code.
— Cloudflare, Agentic Payments
Cloudflare’s Agents SDK supports this through two protocols on 402:
- A client requests a resource or calls a tool.
- The server returns a payment challenge (amount, method, recipient).
- The client pays and retries with a payment credential.
- The server verifies (optionally via a facilitator) and returns the resource plus a receipt.
No pre-created service account or pre-shared API key is required. Agents handle the payment exchange programmatically.
— Cloudflare, Agentic Payments
AWS applies the same status code at the edge for bots: the Monetize action on WAF Bot Control returns an HTTP 402 challenge so the client can pay and resubmit (CloudFront web ACLs only, with MonetizationConfig). See AWS WAF Bot Control.
x402 protocol vs MPP: which HTTP 402 payment standard to use
Section titled “x402 protocol vs MPP: which HTTP 402 payment standard to use”Both standards reuse 402 but differ in headers, payment rails, and breadth.
x402: stablecoin-focused headers and facilitators
Section titled “x402: stablecoin-focused headers and facilitators”x402 ↗︎ is a payment standard created by Coinbase. It uses on-chain stablecoin payments (USDC on Base, Ethereum, Solana, and other networks) and defines three HTTP headers — PAYMENT-REQUIRED, PAYMENT-SIGNATURE, and PAYMENT-RESPONSE — to carry challenges, credentials, and receipts. Servers can offload verification and settlement to a facilitator service so they do not need direct blockchain connectivity.
— Cloudflare, Agentic Payments
The x402 guide describes the exchange: PAYMENT-REQUIRED on 402, client retry with PAYMENT-SIGNATURE, response with PAYMENT-RESPONSE. Schemes include exact (fixed amount) and upto (authorize a maximum; settle actual usage later). Examples use facilitator URL https://x402.org/facilitator (Charge for HTTP content).
When to prefer x402: USDC-style on-chain settlement, Coinbase facilitator model, Cloudflare Monetization Gateway, AI Gateway Machine Payments, and MCP paidTool / withX402 (Charge for MCP tools).
Machine Payments Protocol (MPP): method-agnostic WWW-Authenticate
Section titled “Machine Payments Protocol (MPP): method-agnostic WWW-Authenticate”Machine Payments Protocol (MPP) ↗︎ is an open payment protocol. It adds the WWW-Authenticate: Payment and Authorization: Payment headers to HTTP 402 responses.
MPP supports multiple payment methods beyond blockchains, including cards (via Stripe) and stablecoins. The mppx SDK supports one-time, usage-based, and recurring payments. MPP is also backwards-compatible with x402: MPP clients can consume existing x402 services without modification.
— Cloudflare, Agentic Payments
MPP (Machine Payments Protocol) defines intents charge, session, and subscription, and maps payment data to HTTP auth headers (MCP uses the same flow over JSON-RPC).
When to prefer MPP: You need cards or multiple methods, mppx middleware on Workers, or clients that should speak both MPP and legacy x402 without changing existing x402 servers (Pay from the Agents SDK (MPP)).
| Aspect | x402 | MPP |
|---|---|---|
| Challenge headers | PAYMENT-REQUIRED, PAYMENT-SIGNATURE, PAYMENT-RESPONSE |
WWW-Authenticate: Payment, Authorization: Payment |
| Typical methods | On-chain stablecoins (USDC) | Stablecoins, cards (Stripe), custom |
| Cloudflare server helpers | x402-hono, withX402, paidTool, proxies |
mppx Hono middleware, MCP wrappers |
| Client compatibility | x402 SDKs, withX402Client |
mppx client (can pay x402 challenges) |
Common conclusion: Pick x402 when your stack already uses Cloudflare gateway products, facilitators, and USDC networks. Pick MPP when you want a broader payment-method surface and IETF-style HTTP authentication headers, while still reaching x402-only origins via compatible clients.
Accept MPP payments on a Worker route (mppx.charge)
Section titled “Accept MPP payments on a Worker route (mppx.charge)”For a Worker you control, Cloudflare documents mppx with Hono (Accept payments with MPP).
Prerequisites: Cloudflare account, payment recipient, and MPP secret key. Examples often use a stablecoin on testnet; other methods are covered under MPP payment methods on the same doc set.
- Install dependencies:
honoandmppx(see Accept payments with MPP). - Store
MPP_SECRET_KEYwithwrangler secret put MPP_SECRET_KEY. - Register
mppx.charge({ amount, currency, description, recipient })before the paid route handler. - Deploy with
wrangler deployand test: unpaidcurlshould get 402 andWWW-Authenticate: Payment; paid retry should hit the handler and return a Payment-Receipt header.
Request the paid route without a payment:
The Worker returns 402 Payment Required and a WWW-Authenticate: Payment header. A paid retry reaches the handler and returns a Payment-Receipt header.
— Cloudflare, Accept payments with MPP
Integration choices from the same page: mpp-proxy (gate origin without code changes), Worker route middleware, or MCP tool gating (Accept payments with MPP).
Charge for HTTP content: mpp-proxy and x402-proxy Workers
Section titled “Charge for HTTP content: mpp-proxy and x402-proxy Workers”When the origin must stay unchanged, place a payment Worker in front of the backend.
MPP mpp-proxy
Section titled “MPP mpp-proxy”The mpp-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 an MPP payment challenge. After the client pays, the proxy verifies the payment, forwards the request to your origin, and issues a 1-hour session cookie.
— Cloudflare, Charge for HTTP content (MPP)
Configure PROTECTED_PATTERNS in wrangler.jsonc (pattern, amount, description), set PAY_TO, and deploy from github.com/cloudflare/mpp-proxy with secrets JWT_SECRET and MPP_SECRET_KEY (Charge for HTTP content (MPP)).
x402 x402-proxy
Section titled “x402 x402-proxy”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.
— Cloudflare, Charge for HTTP content (x402)
Use NETWORK (e.g. base-sepolia for testing, base for production), PROTECTED_PATTERNS with price, or inline paymentMiddleware from x402-hono with facilitator { url: "https://x402.org/facilitator" } (Charge for HTTP content (x402)).
Charge MCP tools per call: paidTool and x402 MCP servers
Section titled “Charge MCP tools per call: paidTool and x402 MCP servers”MCP lets you mix free and paid tools on one server.
The Agents SDK provides paidTool, which registers a tool with x402 payment requirements. Clients pay per tool call, and you can mix free and paid tools in the same server.
— Cloudflare, Charge for MCP tools
Wrap McpServer with withX402, set network, recipient, and facilitator, then call paidTool(name, description, price, inputSchema, …) (Charge for MCP tools).
When a client calls a paid tool without payment, the server returns 402 with payment requirements. The client pays via x402, retries with payment proof, and receives the result.
— Cloudflare, Charge for MCP tools
Buyers use withX402Client on MCP connections (Pay from Agents SDK (x402)) or MppClient.wrap for MPP (Pay from the Agents SDK (MPP)). Restrict auto-pay with acceptPaymentPolicy origins or human approval via onPaymentRequired / onChallenge.
Monetization Gateway: x402 at the domain without custom Worker code
Section titled “Monetization Gateway: x402 at the domain without custom Worker code”Monetization Gateway applies x402 payment requirements to matching traffic. You define the shape of the request to match, how much to charge for it, and the receiving wallet.
— Cloudflare, Monetization Gateway
The product is closed beta; U.S.-based buyers and sellers; request access via the dashboard flow described on Monetization Gateway. Match by URL, headers, query, or caller attributes; charge Everyone or Verified Bots only (Get started).
Payment flow (Monetization Gateway):
- Buyer requests a protected resource.
- Gateway returns x402 payment requirements (
PAYMENT-REQUIREDon 402—see x402 protocol). - Buyer retries with
PAYMENT-SIGNATURE. - Gateway verifies, forwards to origin with
PAYMENT-CONTEXTJWT. - Settlement via Coinbase x402 Facilitator; buyer gets the resource.
Pricing schemes (Monetization rules):
| Scheme | x402 scheme | Behavior |
|---|---|---|
| Fixed | exact | Same price per request; gateway settles the signed amount |
| Variable | upto | Authorize a maximum; origin reports actual via PAYMENT-SETTLEMENT |
Origins must validate PAYMENT-CONTEXT before serving paid content (Payment validation). For upto, set PAYMENT-SETTLEMENT with JSON {"amount":"<atomic units>"} only on successful responses before headers are sent; do not set it on 4xx/5xx.
Minimum rule price: $0.001 (Get started).
AI Gateway Machine Payments: pay inference with Payment-Method: x402
Section titled “AI Gateway Machine Payments: pay inference with Payment-Method: x402”Machine Payments on AI Gateway lets clients pay eligible /ai/run inference from a stablecoin wallet instead of prepaid Unified Billing credits (Machine Payments (x402)).
The Payment-Method: x402 header tells Cloudflare to negotiate payment through the x402 protocol ↗︎. This header is specific to Cloudflare.
— Cloudflare, Machine Payments (x402)
Flow: request with Payment-Method: x402 → 402 with PAYMENT-REQUIRED (often upto max) → client signs → retry with PAYMENT-SIGNATURE → inference runs and cost settles up to the authorized max (Machine Payments (x402)).
Eligibility called out in changelog material: U.S. customers with a credit card on file (Pay for AI inference with Machine Payments). Without the header, AI Gateway uses normal billing and may deduct Unified Billing credits (Unified Billing).
Agents SDK x402 v2 migration: packages and network IDs
Section titled “Agents SDK x402 v2 migration: packages and network IDs”If you integrate MCP payments with older x402 packages, Cloudflare migrated to @x402/core and @x402/evm v2 (Agents SDK v0.4.0 changelog):
- Replace peer dependency
x402with@x402/coreand@x402/evm. PaymentRequirementsuses v2 fields (e.g.amountinstead ofmaxAmountRequired).X402ClientConfig.accountisClientEvmSigner(compatible withprivateKeyToAccount()).- Networks accept legacy names (
base-sepolia) or CAIP-2 (eip155:84532);X402ClientConfig.networkis optional (auto-select from requirements). - Servers lazy-init facilitator on first paid tool; clients accept PAYMENT-SIGNATURE (v2) and X-PAYMENT (v1).
Cloudflare Wallets and spend limits (buyer side)
Section titled “Cloudflare Wallets and spend limits (buyer side)”Cloudflare Wallets describe Account Wallets (human-owned) and Virtual Wallets (API-key agents with spend caps delegated from the account). They tie into agentic payments so agents can hold stablecoins and pay within owner limits—see the same page for cloudflare.pay handles and delegation model.
Common mistakes
Section titled “Common mistakes”- Serving paid content without validating
PAYMENT-CONTEXTon Monetization Gateway origins (Payment validation). - Setting
PAYMENT-SETTLEMENTon error responses or for exact fixed pricing (gateway settles fixed amounts itself). - Forgetting
Payment-Method: x402on AI Gateway and wondering why credits were charged instead (Unified Billing). - Treating MPP challenge amounts as decimal UI values—they are integer base units (Pay from the Agents SDK (MPP)).
- Using AWS Monetize outside CloudFront-associated web ACLs or without MonetizationConfig (AWS WAF Bot Control).
How to check this on your site
Section titled “How to check this on your site”- Call a protected URL or MCP tool without payment and confirm HTTP 402 plus the expected challenge header (
PAYMENT-REQUIREDfor x402, orWWW-Authenticate: Paymentfor MPP) using the flows in x402 or Accept payments with MPP. - Retry with an x402 or MPP client (
wrapFetchWithPayment,mppxfetch, orwithX402Client) and verify 200 (or tool success) plus receipt headers documented on those pages. - For Monetization Gateway rules, decode
PAYMENT-REQUIREDfrom a test request and confirmscheme,amount, andpayTomatch x402 protocol and your Monetization rules. - For AI Gateway, send
POSTto/ai/runwithPayment-Method: x402per Machine Payments (x402) and confirm the 402 → signed retry sequence. - You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page looks wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.