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.
Use .dev.vars
to set local overrides for environment variables that should not be checked into your repository.
If you want to manage environment-based configuration that you want checked into your repository (for example, non-sensitive or shared environment defaults), you can define environment variables as [vars]
in your Wrangler configuration. 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 npx vite devTerminal window pnpm vite devTerminal window yarn vite 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" -
Specify the environment when running the
dev
command:Wrangler
Terminal window npx wrangler dev --env stagingTerminal window pnpm wrangler dev --env stagingTerminal window yarn wrangler dev --env stagingVite plugin
Terminal window CLOUDFLARE_ENV=staging npx vite devTerminal window CLOUDFLARE_ENV=staging pnpm vite devTerminal window CLOUDFLARE_ENV=staging yarn vite devOnly 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