Environment variables and secrets
During local development, you may need to configure environment variables (such as API URLs, feature flags) and secrets (API tokens, private keys). You can use a .dev.vars
file in the root of your project to override environment variables for local development, and both Wrangler and the Vite plugin will respect this override.
While .dev.vars
is commonly used for storing secrets, the file is really just a local override for any environment variable — secret or otherwise. This allows you to avoid polluting environment variables in your Wrangler configuration, and provide a way to set local-only values.
You can also define environment variables as [vars]
in your Wrangler configuration (for development
, staging
, production
, etc). This is a good way to manage environment-based configuration that you want checked into your repository (for example, non-sensitive or shared environment defaults). Using a .dev.vars
file is specifically for local-only secrets or configuration that you do not want in version control and only want to inject in local dev sessions.
-
Create a
.dev.vars
file in your project root. -
Add key-value pairs:
.dev.vars API_HOST="localhost:3000"DEBUG="true"SECRET_TOKEN="my-local-secret-token" -
Run your
dev
commandWrangler
Terminal window npx wrangler devTerminal window pnpm wrangler devTerminal window yarn wrangler devVite plugin
Terminal window npm run devTerminal window pnpm run devTerminal window yarn run dev
To simulate different local environments, you can:
-
Create a file named
.dev.vars.<environment-name>
. For example, we'll use.dev.vars.staging
. -
Add key-value pairs:
.dev.vars.staging API_HOST="staging.localhost:3000"DEBUG="false"SECRET_TOKEN="staging-token" -
Run your
dev
command with an--env
flag:Wrangler
Terminal window npx wrangler dev --env stagingTerminal window pnpm wrangler dev --env stagingTerminal window yarn wrangler dev --env stagingVite plugin
Terminal window npm run dev --env stagingTerminal window pnpm run dev --env stagingTerminal window yarn run dev --env stagingOnly the values from
.dev.vars.staging
will be applied instead of.dev.vars
.
- To learn how to configure multiple environments in Wrangler configuration, read the documenation.
- To learn how to use Wrangler environments and Vite environments together, read the Vite plugin documentation
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark