PaidOnChain

USDC paywall on Cloudflare Workers

A Worker plus one KV namespace is enough to sell a file for USDC, all on the free tier.

The Worker

import { verifyPayment } from "paidonchain";
const PAY_TO = "0xYourAddress", PRICE = "19";

export default {
  async fetch(req, env) {
    const url = new URL(req.url);
    if (req.method === "POST" && url.pathname === "/claim") {
      const { chain = "base", tx } = await req.json();
      const lock = `tx:${chain}:${String(tx).toLowerCase()}`;
      if (await env.SALES.get(lock)) return Response.json({ error: "tx already used" }, { status: 409 });
      const r = await verifyPayment({ chain, tx, to: PAY_TO, minAmount: PRICE });
      if (!r.ok) return Response.json(r, { status: 402 });
      const token = crypto.randomUUID();
      await env.SALES.put(lock, "1");
      await env.SALES.put("tok:" + token, "1", { expirationTtl: 7 * 86400 });
      return Response.json({ download: "/download/" + token });
    }
    const m = url.pathname.match(/^\/download\/([\w-]+)$/);
    if (m && await env.SALES.get("tok:" + m[1])) return env.ASSETS.fetch(new Request(url.origin + "/product.zip"));
    return new Response("Not found", { status: 404 });
  },
};

Why server-side matters

A static page that "checks" payments in the browser can be bypassed in seconds. Here the file is only served after the Worker has read the receipt from the chain itself.

Known limits

Want a ready-made version with a sales page? See the No-Bank Checkout Kit.

More guides