Safeguard
Developer Tools•

Cloudflare Workers secrets versus vars: which to use

In Cloudflare Workers a secret is an environment variable whose value is hidden after you set it. Here is when to use vars, secrets and local dev files.

Safeguard Security Team
3 min read

Use vars for plain configuration and secrets for anything sensitive. A Worker reads both the same way through its env object, but a secret's value is hidden in Wrangler and the Cloudflare dashboard once you define it, while a var sits in plain text in your configuration file.

The same mechanism, different visibility

Cloudflare's documentation states that secrets are environment variables. The difference is visibility: after you create a secret, its value is not shown in Wrangler or the dashboard. Your Worker sees no difference between the two.

Environment variables defined in the Wrangler configuration, under vars, are not encrypted. They can be text or JSON values, and the documentation warns not to use plaintext environment variables for sensitive information. Because they live in a file you commit, anything placed there ends up in version control.

How to set a secret

  • With Wrangler, run wrangler secret put followed by the key name. This creates a new Worker version and deploys it immediately.
  • If you use gradual deployments, run wrangler versions secret put and deploy the version separately.
  • In the dashboard, open the Worker, go to Settings, then Variables and Secrets, add one and choose the Secret type.
  • For many values, a secrets file can upload up to 100 secrets alongside your code during deploy or version upload.

Local development

Locally, put secrets in a .dev.vars or .env file next to your Wrangler configuration, using dotenv syntax. Use only one of the two: if .dev.vars exists, .env values are ignored. Add both patterns to .gitignore so they are never committed. For different environments, files named with the environment suffix are supported.

A decision table in words

  1. Public hostname, feature flag, log level: vars.
  2. API key, token, signing key, database password: secret.
  3. Access to another Cloudflare resource such as a KV namespace or R2 bucket: neither. Use a binding, which needs no key at all (see bindings instead of API keys).

Pitfalls

  • Vars are not inherited between Wrangler environments, so each environment needs its own block. A missing value then shows up as undefined at runtime, which can silently disable a check.
  • A secret that was once committed in a vars block is exposed in history even after you move it. Rotate it.
  • Changing a secret creates a new version. Plan for that when you use gradual deployments.
  • Do not log the whole env object while debugging.

Quick check

Can I read a secret back from the dashboard later?

No. Its value is hidden in Wrangler and the dashboard after you set it. Keep the source of truth in your password manager or secrets store.

Is a var safe for a public API base URL?

Yes. Vars are fine for non-sensitive configuration. Just remember they are stored unencrypted.

Safeguard's free scan can check the packages your Worker bundles for known vulnerabilities.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.