Environment variables
Environment variables are a type of binding that allow you to attach text strings or JSON values to your Worker. They are passed in via the env
parameter in your Worker's fetch
event handler.
Text strings and JSON values are not encrypted and are useful for storing application configuration. If you need to store sensitive information (such as API keys or tokens), use secrets.
Text and JSON values are defined via the [vars]
configuration in your Wrangler file. In the following example, API_HOST
and API_ACCOUNT_ID
are text values and SERVICE_X_DATA
is a JSON value.
{ "name": "my-worker-dev", "vars": { "API_HOST": "example.com", "API_ACCOUNT_ID": "example_user", "SERVICE_X_DATA": { "URL": "service-x-api.dev.example", "MY_ID": 123 } }}
name = "my-worker-dev"
[vars]API_HOST = "example.com"API_ACCOUNT_ID = "example_user"SERVICE_X_DATA = { URL = "service-x-api.dev.example", MY_ID = 123 }
Refer to the following example on how to access the API_HOST
environment variable in your Worker code:
export default { async fetch(request, env, ctx) { return new Response(`API host: ${env.API_HOST}`); },};
export interface Env { API_HOST: string;}
export default { async fetch(request, env, ctx): Promise<Response> { return new Response(`API host: ${env.API_HOST}`); },} satisfies ExportedHandler<Env>;
Since vars
is a non-inheritable key, you need to explicitly define your environment variables for each environment (for example, staging
and production
).
{ "name": "my-worker-dev", "env": { "staging": { "vars": { "API_HOST": "staging.example.com", "API_ACCOUNT_ID": "staging_example_user", "SERVICE_X_DATA": { "URL": "service-x-api.dev.example", "MY_ID": 123 } } }, "production": { "vars": { "API_HOST": "production.example.com", "API_ACCOUNT_ID": "production_example_user", "SERVICE_X_DATA": { "URL": "service-x-api.prod.example", "MY_ID": 456 } } } }}
name = "my-worker-dev"
[env.staging.vars]API_HOST = "staging.example.com"API_ACCOUNT_ID = "staging_example_user"SERVICE_X_DATA = { URL = "service-x-api.dev.example", MY_ID = 123 }
[env.production.vars]API_HOST = "production.example.com"API_ACCOUNT_ID = "production_example_user"SERVICE_X_DATA = { URL = "service-x-api.prod.example", MY_ID = 456 }
When running wrangler dev
, variables in the Wrangler configuration file are automatically overridden by any values defined in a .dev.vars
file located in the root directory of your Worker. This is useful for providing values you do not want to check in to source control.
API_HOST = "localhost:4000"API_ACCOUNT_ID = "local_example_user"
Alternatively, you can specify per-environment values in the Wrangler configuration file and provide an environment
value via the --env
flag when developing locally:
wrangler dev --env=local
To add environment variables via the dashboard:
- Log in to Cloudflare dashboard ↗ and select your account.
- Select Workers & Pages.
- In Overview, select your Worker.
- Select Settings.
- Under Variables and Secrets, select Add.
- Select a Type, input a Variable name, and input its Value. This variable will be made available to your Worker.
- (Optional) To add multiple environment variables, select Add variable.
- Select Deploy to implement your changes.
When to use secrets
If your environment variable is a secret (such as a password or API token), select the Secret type when adding it via the dashboard or use Wrangler's built-in command:
wrangler secret put <KEY>
Secrets function similarly to environment variables in a Worker, but with crucial differences:
-
Visibility: Once you define a secret, its value is no longer visible in Wrangler or the Cloudflare dashboard.
-
Security: Sensitive data, such as passwords and tokens, should always be encrypted to prevent accidental exposure.
To your Worker, there is no difference between an environment variable and a secret. The secret's value is passed through as defined.
When to use plaintext environment variables
Plaintext environment variables are best for non-sensitive configuration details, such as hostnames and IDs. These are values that do not require encryption because leaking them does not compromise security or privacy.
- Learn how to access environment variables in ES modules syntax for an optimized experience.
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