x402 payment middleware for Cloudflare Workers — POC 1 of the Stable402 reference implementation series
x402-worker-middleware is an early-stage TypeScript project in the AI payments / x402 ecosystem, focused on agentic-payments, base-sepolia, cloudflare-workers, hono. It currently has 1 GitHub stars and 0 forks, and sits alongside related tools like stable402.com, sample-agentic-serverless-payments, strale, toll-booth, routex, polymarket-liquidity-api.
POC 1 — Stable402
A Cloudflare Worker that gates a JSON API endpoint behind x402 — the HTTP 402 payment protocol for machine-to-machine commerce.
An AI agent (or any HTTP client) sends a request → receives HTTP 402 with payment requirements → signs with a CDP wallet → retries with payment → receives the content. Settlement on Base Sepolia.
→ Reference page: stable402.com/demos/gate
Client Worker (this repo) CDP Facilitator
│ │ │
├─ GET /gate ─────────────────→│ │
│ ├─ 402 + PAYMENT-REQUIRED ─────→│
│←─ HTTP 402 ──────────────────┤ (base64 PaymentRequired) │
│ PAYMENT-REQUIRED header │ │
│ │ │
├─ GET /gate ─────────────────→│ │
│ PAYMENT-SIGNATURE header ├─ POST /verify ───────────────→│
│ (EIP-3009 authorization) │←─ verification result ────────┤
│ │ │
│←─ HTTP 200 ──────────────────┤
│ PAYMENT-RESPONSE header │
│ + JSON payload │
Stack:
@x402/hono — paymentMiddlewareFromConfig@x402/core — HTTPFacilitatorClientNetwork: Base Sepolia testnet (eip155:84532)
Asset: USDC on Base Sepolia (0x036CbD53842c5426634e7929541eC2318f3dCF7e)
Price: 1000 base units = $0.001 USDC
npm install -g wrangler)npm install
npx wrangler secret put WALLET_ADDRESS
# Enter your Base Sepolia wallet address when prompted
# e.g. 0x22F637cF55217cb00252dDCF0c61FC4EfC12682c
npm run dev
Test locally (expects HTTP 402 — no payment signature):
curl -i http://localhost:8787/gate
npm run deploy
Step 1 — Confirm the gate is working (expect HTTP 402):
curl -i https://api.stable402.com/gate
You should receive:
HTTP/2 402
payment-required: <base64-encoded PaymentRequired object>
Step 2 — Decode the payment requirements:
curl -s https://api.stable402.com/gate -o /dev/null -D - | \
grep -i payment-required | \
awk '{print $2}' | \
base64 -d | jq .
Step 3 — Pay and access (requires a configured CDP wallet client — see Coinbase x402 Quickstart for Buyers)
paymentMiddlewareFromConfig per-request pattern
In Cloudflare Workers, env bindings (including WALLET_ADDRESS) are only available at request time, not at module load. This Worker creates the middleware inside app.use('/gate', ...) to access c.env.WALLET_ADDRESS. HTTPFacilitatorClient is lightweight and safe to instantiate per-request.
syncFacilitatorOnStart: false
Cloudflare Workers are stateless and ephemeral — there is no meaningful "startup" to sync with. Setting this to false skips the initialization fetch to the CDP facilitator.
Price as AssetAmount
Using { asset: USDC_ADDRESS, amount: '1000' } instead of a money string makes the exact token and base-unit amount explicit — important for a reference implementation where the encoding must be transparent.
x402-worker-middleware/
├── src/
│ └── index.ts # Hono app with paymentMiddlewareFromConfig
├── wrangler.toml # Worker config (routes api.stable402.com/*)
├── package.json
├── tsconfig.json
└── README.md
This repo is POC 1 in the Stable402 reference implementation series for the x402 agentic payment ecosystem. Every POC ships with a companion reference page explaining the architecture, code, and protocol decisions in depth.
Stable402.com — reference site for the x402 agentic payment ecosystem. Built with Astro 6 + Starlight on Cloudflare Pages.
An open-source, serverless reference implementation for agentic commerce: pay-per-use generative AI, physical goods, and digital goods, where embedded wallets and autonomous AI agents settle USDC stablecoin micropayments over the x402 protocol using Amazon Bedrock AgentCore payments on AWS
Trust and quality infrastructure for AI agents. 225+ quality-scored capabilities, SQS scoring, EU AI Act support.
Any API becomes a Lightning toll booth in one line. L402 middleware for Express, Hono, Deno, Bun, and Workers.
Multi-chain settlement cost router for the x402 payment protocol. Selects the cheapest or fastest blockchain for each AI agent micropayment.
Real-time Polymarket prediction market liquidity data via x402 micropayments. Order book depth, spread analysis, and market efficiency scoring. Pay $0.005 USDC per request on Base. No API key required.
Client SDK, Claude plugin and skill framework for the Pinion protocol. x402 micropayments on Base.
Building blocks for Agentic payments (x402, MPP, AP2) for TypeScript, Rust, Go, Python, Ruby, PHP, Lua, Kotlin and Swift.
Building blocks for Agentic payments (x402, MPP, AP2) for TypeScript, Rust, Go, Python, Ruby, PHP, Lua, Kotlin and Swift.
Turn any API or MCP server into a paid service with x402
Public adapters and discovery catalog for Triptych OS (Agent OS): agent frameworks, MCP/A2A/x402 protocols, workflows, wallets, SDKs, and examples for execute-first routing, governed handoffs, and receipt-aware agent commerce.
A wallet for agents. Make payments via x402, use stablecoins, swap assets, earn yield with defi and buy tokenized stocks across the most popular chains.