Introduction
Digital creators selling ebooks, courses, templates, fonts, or other downloadable assets need two primitives: durable object storage and an instant delivery path to buyers. Cloudflare R2 supplies both while eliminating platform fees, payout delays, and geographic restrictions common in managed storefronts. This article details a production-grade architecture that stores files in R2, generates single-use signed URLs, and integrates onchain payment verification—without Stripe, Gumroad, or Ko-fi.
The system is built from two components: an RPC-style API running on Cloudflare Workers that handles payment checks and URL signing, and a React storefront that invokes that API. All traffic flows through the edge, latency is under 100 ms worldwide, and storage cost is $0.015/GB/month with no egress charges.
Storage Layer: R2 Buckets
Create a single R2 bucket with two public subfolders: products and receipts. Products holds encrypted files named by SHA-256 hashes; receipts stores IPFS hashes of on-chain receipts for fraud checks. Configure a lifecycle rule that transitions data to Glacier after 90 days to reduce long-term cost. CORS headers restrict upload to Workers only; Cloudflare Access can gate uploads further if you need multi-team workflows.
Bucket naming matters. Use a reverse-DNS style, e.g., com.example.digital.store, so wildcard certificates from Cloudflare cover *.example.com and eliminate TLS renewal logic. Enable versioning to protect against accidental overwrites; only turn on MFA delete if you need strict two-person control.
Payment Verification via Onchain Receipts
Buyers pay via Lightning or Bitcoin; the storefront emits an invoice and waits for onchain confirmation. Once confirmed, the storefront POSTs the txid to your Worker API. The Worker calls the Lightning node RPC or a chain indexer to fetch the raw transaction and checks:
- transaction is confirmed in the last 3 blocks - outputs include a specific OP_RETURN or memo field you control - amount meets or exceeds product price in sats
Only after passing checks does the Worker sign a time-bound (30 minutes) R2 signed URL and return it to the caller. This removes credit-card network latency, KYC delays, and Stripes 7% marketplace fee. The Worker caches the signed URL in KV so repeat buyers skip rediscovery.
For recurring payments, store a Lightning invoice in KV keyed by customer pubkey; the Worker cancels the subscription in KV when the payment horizon expires, revoking future signed URLs automatically.
Edge Delivery: Signed URLs and Caching
When the storefront receives a signed URL, it redirects the browser with a 302. The URL contains an HMAC-signed query string that R2 validates: /products/a1b2c3?expires=1735689600&signature=sha256_hmac. The HMAC secret lives in Workers secrets—never exposed to the storefront code.
Edge caching uses Cache API. The Worker caches the signed URL for 5 minutes so reloading the page does not re-contact the origin. R2 itself caches objects at the edge for 24 hours; if you push daily updates, use versioned filenames or cache-busting query strings to force freshness.
Clean API Design for Headless Storefronts
The Worker exposes four endpoints under /v1:
GET /v1/products Returns a JSON array of product SKUs, names, prices, and version hashes. The version hash is the SHA-256 of the file in R2; clients use it to detect stale cached assets.
POST /v1/checkout Body: {sku, pubkey} Returns {invoice, expires_at} for Lightning or {address, amount} for Bitcoin. The Worker writes the pubkey to KV under a checkout ID to prevent double-spend replay.
POST /v1/verify Body: {checkout_id, txid} Returns {url, filename} if payment is valid; otherwise HTTP 402. The Worker checks onchain state and signs the URL only once.
GET /v1/receipts/{txid} Returns the raw transaction JSON and IPFS CID of a receipt. Used for accounting and support tickets.
All endpoints use HTTP signatures for authentication between the storefront and Worker; the storefront signs requests with its private key, and the Worker verifies against a public key stored in secrets.
Deployment and Observability
Deploy the Worker via Wrangler using GitHub Actions. The CI pipeline runs:
- wrangler deploy --minify --env production - Playwright e2e tests that create a Lightning invoice, mine a block in regtest, and assert the signed URL appears - Lighthouse checks against the storefront to ensure 95+ performance score
Observability is Cloudflare-native. Use Workers Logs, R2 object logs, and a single Grafana dashboard fed by Cloudflare Logpush. Metrics to watch: 95th percentile Worker latency, KV read/write count, R2 egress bytes, and invoice confirmation time.
Security relies on three layers: Cloudflare Access for uploads, HMAC-signed URLs for downloads, and onchain verification for payments. Rotate secrets every 90 days with a CLI script that updates them via Wrangler secret put.
Conclusion
Stripping middlemen from digital product sales does not mean sacrificing reliability or speed. Cloudflare R2 supplies the storage, Workers supply the compute, and onchain receipts supply instant settlement. The architecture delivers sub-second signed URLs worldwide at a cost of pennies per GB, with no geographic