Skip to content

Environment variables and secrets

Put secrets for use in local development in either a .dev.vars file or a .env file, in the same directory as the Wrangler configuration file.

These files should be formatted using the dotenv ↗︎ syntax. For example:

.dev.vars / .envbash
SECRET_KEY="value"
API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"

When secrets.required is configured alongside vars, local files can supply both declared secrets and overrides for configured vars, while unrelated keys are ignored:

{
	"vars": {
		"LOG_LEVEL": "info",
	},
	"secrets": {
		"required": ["API_TOKEN"],
	},
}
[vars]
LOG_LEVEL = "info"

[secrets]
required = [ "API_TOKEN" ]
.env / .dev.varsbash
API_TOKEN="local-token"     # Loaded (declared in secrets.required)
LOG_LEVEL="debug"           # Loaded (overrides configured vars.LOG_LEVEL)
UNDECLARED_KEY="ignored"    # Excluded (not in secrets.required or vars)

To set different secrets for each Cloudflare environment, create files named .dev.vars.<environment-name> or .env.<environment-name>.

When you select a Cloudflare environment in your local development, the corresponding environment-specific file will be loaded ahead of the generic .dev.vars (or .env) file.

  • When using .dev.vars.<environment-name> files, all secrets must be defined per environment. If .dev.vars.<environment-name> exists then only this will be loaded; the .dev.vars file will not be loaded.
  • In contrast, all matching .env files are loaded and the values are merged. For default file selection, each key takes the value from the highest-precedence file that defines it, in the following order (highest to lowest):
    • .env.<environment-name>.local (most specific)
    • .env.<environment-name>
    • .env.local
    • .env (least specific)

Basic setup

Here are steps to set up environment variables for local development using either .dev.vars or .env files.

  1. Create a .dev.vars / .env file in your project root.

  2. Add key-value pairs:

    .dev.vars/.envini
    API_HOST="localhost:3000"
    DEBUG="true"
    SECRET_TOKEN="my-local-secret-token"
  3. Run your dev command

    Wrangler

    npx wrangler dev

    Vite plugin

    npx vite dev

Multiple local environments

To simulate different local environments, you can provide environment-specific files. For example, you might have a staging environment that requires different settings than your development environment.

  1. Create a file named .dev.vars.<environment-name>/.env.<environment-name>. For example, we can use .dev.vars.staging/.env.staging.

  2. Add key-value pairs:

    .dev.vars.staging/.env.stagingini
    API_HOST="staging.localhost:3000"
    DEBUG="false"
    SECRET_TOKEN="staging-token"
  3. Specify the environment when running the dev command:

    Wrangler

    npx wrangler dev --env staging

    Vite plugin

    CLOUDFLARE_ENV=staging npx vite dev
    • If using .dev.vars.staging, only the values from that file will be applied instead of .dev.vars.
    • If using .env.staging, the values will be merged with .env files, with the most specific file taking precedence.

Learn more

Was this helpful?