D1, R2 & Resend · Free guide

Keeping Resend keys, D1 bindings, and R2 private buckets secure on static sites

Store zero API secrets inside astro.config.mjs, src/, or public/. Keep private keys like RESEND_API_KEY, TURNSTILE_SECRET, and HMAC signing secrets strictly inside encrypted Cloudflare environment variables (or gitignored .dev.vars locally), restrict Turnstile hostnames explicitly, and bind D1/R2 directly through Cloudflare dashboard bindings.

By StaticLaunch · Four Media Group LLC

Keep client env vars vs. Worker secrets strictly separated

Notice in Astro how PUBLIC_-prefixed variables bake publicly into browser bundles—perfect for public Turnstile sitekeys, forbidden for API tokens, webhooks, or email secrets.

  • PUBLIC_TURNSTILE_SITE_KEY -> Safe inside client HTML.
  • TURNSTILE_SECRET -> Server-only Cloudflare secret.
  • RESEND_API_KEY / EMAIL_SERVICE_TOKEN -> Server-only encrypted secret.
  • SUBSCRIBER_RECEIPT_SECRET -> Server-only secret for HMAC download receipts.

Gitignore .dev.vars and scan before every push

Local Wrangler emulation reads secrets from .dev.vars—verify .dev.vars* sits permanently inside .gitignore and run static secret checks before pushing commits to GitHub.

Enforce Turnstile hostname & action allowlists in API Functions

Even with valid tokens from another site, reject verification unless Cloudflare's siteverify response matches both your expected hostname (yourdomain.com) and exact action string.

Common questions

If I accidentally commit a Resend key or .dev.vars once and delete it in the next commit, is it safe?

No! Git preserves old commits permanently. Immediately revoke and rotate the leaked secret inside Cloudflare/Resend, then clean repository history carefully.

Can D1 bindings be guessed externally from browser URLs?

No. D1 bindings exist solely inside Cloudflare's server Worker runtime env object—browsers can only hit routes you intentionally expose under functions/.

References

Provider interfaces and limits can change. Consult the original documentation before changing production settings.