Environments
Wrangler allows you to use environments to create different configurations for the same Worker application. Environments are configured in the Worker's Wrangler configuration file.
When you create an environment, Cloudflare effectively creates a new Worker with the name <top-level-name>-<environment-name>. For example, a Worker project named my-worker with an environment dev would deploy as a Worker named my-worker-dev.
Review the following environments flow:
-
Create a Worker, named
my-workerfor example. -
Create an environment, for example
dev, in the Worker's Wrangler configuration file, by adding a[env.<ENV_NAME>]section.{"name": "my-worker","env": {"<ENV_NAME>": {// environment-specific configuration goes here}}}name = "my-worker"[env]"<ENV_NAME>" = { } -
You can configure the
devenvironment with different values to the top-level environment. Refer here for how different options are inherited - or not inherited - between environments. For example, to set a different route for a Worker in thedevenvironment:{"name": "your-worker","route": "example.com","env": {"dev": {"route": "dev.example.com"}}}name = "your-worker"route = "example.com"[env.dev]route = "dev.example.com" -
Environments are used with the
--envor-eflag on Wrangler commands. For example, you can develop the Worker in thedevenvironment by runningnpx wrangler dev -e=dev, and deploy it withnpx wrangler deploy -e=dev.
Non-inheritable keys are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.
For example, bindings and environment variables are non-inheritable, and must be specified per environment in your Wrangler configuration file.
Review the following example Wrangler file:
{ "name": "my-worker", "vars": { "API_HOST": "example.com" }, "kv_namespaces": [ { "binding": "<BINDING_NAME>", "id": "<KV_NAMESPACE_ID_DEV>" } ], "env": { "production": { "vars": { "API_HOST": "production.example.com" }, "kv_namespaces": [ { "binding": "<BINDING_NAME>", "id": "<KV_NAMESPACE_ID_PRODUCTION>" } ] } }}name = "my-worker"
vars = { API_HOST = "example.com" }
kv_namespaces = [ { binding = "<BINDING_NAME>", id = "<KV_NAMESPACE_ID_DEV>" }]
[env.production]
vars = { API_HOST = "production.example.com" }
kv_namespaces = [ { binding = "<BINDING_NAME>", id = "<KV_NAMESPACE_ID_PRODUCTION>" }]To use a service binding that targets a Worker in a specific environment, you need to append the environment name to the target Worker name in the service field. This should be in the format <worker-name>-<environment-name>.
In the example below, we have two Workers, both with a staging environment. worker-b has a service binding to worker-a. Note how the service field in the staging environment points to worker-a-staging, whereas the top-level service binding points to worker-a.
{ "name": "worker-a", "vars": { "FOO": "<top-level-var>" }, "env": { "staging": { "vars": { "FOO": "<staging-var>" } } }}name = "worker-a"
vars = { FOO = "<top-level-var>" }[env.staging.vars]FOO = "<staging-var>"{ "name": "worker-b", "services": { "binding": "<BINDING_NAME>", "service": "worker-a" }, "env": { "staging": { "service": { "binding": "<BINDING_NAME>", "service": "worker-a-staging" } } }}name = "worker-b"
services = { binding = "<BINDING_NAME>", service = "worker-a" }
# Note how `service = "worker-a-staging"`env.staging.service ={ binding = "<BINDING_NAME>", service = "worker-a-staging" }You may assign environment-specific secrets by running the command wrangler secret put <KEY> -env. You can also create dotenv type files named .dev.vars.<environment-name>.
Like other environment variables, secrets are non-inheritable and must be defined per environment.
The following Wrangler file adds two environments, [env.staging] and [env.production], to the Wrangler file. If you are deploying to a Custom Domain or route, you must provide a route or routes key for each environment.
{ "name": "my-worker", "route": "dev.example.com/*", "vars": { "ENVIRONMENT": "dev" }, "env": { "staging": { "vars": { "ENVIRONMENT": "staging" }, "route": "staging.example.com/*" }, "production": { "vars": { "ENVIRONMENT": "production" }, "routes": [ "example.com/foo/*", "example.com/bar/*" ] } }}name = "my-worker"route = "dev.example.com/*"vars = { ENVIRONMENT = "dev" }
[env.staging]vars = { ENVIRONMENT = "staging" }route = "staging.example.com/*"
[env.production]vars = { ENVIRONMENT = "production" }routes = [ "example.com/foo/*", "example.com/bar/*"]You can pass the name of the environment via the --env flag to run commands in a specific environment.
With this configuration, Wrangler will behave in the following manner:
npx wrangler deployUploaded my-workerPublished my-worker dev.example.com/*npx wrangler deploy --env stagingUploaded my-worker-stagingPublished my-worker-staging staging.example.com/*npx wrangler deploy --env productionUploaded my-worker-productionPublished my-worker-production example.com/*Any defined environment variables (the vars key) are exposed as global variables to your Worker.
With this configuration, the ENVIRONMENT variable can be used to call specific code depending on the given environment:
if (ENVIRONMENT === "staging") { // staging-specific code} else if (ENVIRONMENT === "production") { // production-specific code}To deploy your code to your *.workers.dev subdomain, include workers_dev = true in the desired environment. Your Wrangler file may look like this:
{ "name": "my-worker", "route": "example.com/*", "env": { "staging": { "workers_dev": true } }}name = "my-worker"route = "example.com/*"
[env.staging]workers_dev = trueWith this configuration, Wrangler will behave in the following manner:
npx wrangler deployUploaded my-workerPublished my-worker example.com/*npx wrangler deploy --env stagingUploaded my-workerPublished my-worker https://my-worker-staging.<YOUR_SUBDOMAIN>.workers.devWas 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