Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

How we built a pay‑per‑call x402 API on the Cloudflare Workers free plan (and only charge on success)

Description and use

ScrapeMole successfully implemented a pay‑per‑call API on Cloudflare Workers free plan, ensuring only successful calls are charged, handling regional bot checks, CPU limits, and shared I/O issues.

Problem

Agents need a way to pay for API calls without manual account setup, while ensuring failed calls are not charged.

Outcome

ScrapeMole successfully implemented a pay‑per‑call API on Cloudflare Workers free plan, ensuring only successful calls are charged, handling regional bot checks, CPU limits, and shared I/O issues.

Workflow

  • 1. Client requests paid endpoint and receives HTTP 402 with payment details.
  • 2. Client signs EIP‑3009 transferWithAuthorization and retries with PAYMENT‑SIGNATURE header.
  • 3. Server forwards payment to facilitator (PayAI) for verification before work.
  • 4. Server processes request; on success returns 200 and settles payment; on error returns JSON with "charged":false.
  • 5. For blocked egress, retry request inside Durable Object with locationHint to change region.
  • 6. If parsing exceeds 10 ms, offload to Vercel function.
  • 7. Cache facilitator data after first successful request to avoid shared I/O.
  • 8. Run end‑to‑end tests on Base Sepolia, comparing on‑chain balances to verify charging logic.

Evidence

The server sends the payment to the facilitator (we use PayAI) for verification before doing any work, and requests settlement only after the handler has returned a 200.

Source

We test this end to end on Base Sepolia with throwaway wallets, comparing on‑chain USDC balances before and after the run. The latest run passed 13/13 checks... The buyer's balance went down by exactly $0.035, the sum of the four successful calls. The failing calls cost $0

Source

When a run comes back BLOCKED, re‑run it inside a Durable Object created with a locationHint, which places it, and its outbound requests, in another region.

Source

  • Record date: 2026-10-11T20:10:54.602Z