Where secrets live

← Previous Next →
Site Operations · 2 of 6

Where secrets live

Runtime variables and Build variables are two different boxes with the same label, and only one of them reaches a running request.

Key rule

Cloudflare has two settings screens both labelled Variables and secrets. The one nested under Build feeds only the CI process. Runtime code reads the other one. A key in the wrong box is invisible to your Worker.

Cloudflare's own documentation states it plainly: "Build variables will not be accessible at runtime." A static site like this one has no build step that needs any secret, so a key placed there is silently useless. The build succeeds, the deploy succeeds, and the running Worker sees nothing.

The symptom is distinctive. The endpoint routes correctly but returns 500 with the message the code emits when it finds no key. That is a configuration problem, not a crash. A 404 means routing; a 500 with a clear message means configuration.

Setting one correctly

  • Dashboard, then the project, then Settings, then the top-level Variables and secrets, not the one inside Build.
  • Choose type Secret, not Text, so the value is encrypted and cannot be read back afterwards.
  • Apply it to Production. Preview is a separate environment with its own values.
  • Redeploy. Variable changes only affect deployments created after they are saved. The running deployment keeps whatever it was built with.

What this site expects

NameRequiredPurpose
RESEND_API_KEYYesSending all outbound mail
TURNSTILE_SECRET_KEYNoBot verification; skipped entirely when unset
CONTACT_TONoOverrides the delivery address
CONTACT_FROMNoOverrides the contact sender
NEWSLETTER_FROMNoOverrides the newsletter sender

Secrets never belong in files

This site previously kept credentials in a .env file. On HostGator an .htaccess rule blocked access to dotfiles, so it was protected. Cloudflare ignores .htaccess entirely, because that is an Apache mechanism. After the migration the file was publicly downloadable at /.env, in plaintext, containing a working SMTP password and an API key.

Both credentials were rotated and the file was removed from version control. The lesson generalizes: a protection that lives in your host's configuration does not travel with your files when you change hosts.

Habit

Secrets belong in something built to hold them: the Cloudflare dashboard, or a password manager. Not a file in the repo, not a chat transcript, not a code comment. Anywhere else and you are relying on nobody looking.

The Turnstile site key is the exception and is public by design. It appears in page source because the browser needs it. Only the matching secret key is sensitive.