In the Workers platform, environment variables, secrets, and KV namespaces are known as bindings. Regardless of type, bindings are always available as global variables within your Worker script.
Environment variables via wrangler
Adding environment variables via wrangler
Environment variables are defined via the
[vars] configuration in your
wrangler.toml file and are always plaintext values.
These environment variables can then be accessed within your Worker script as global variables. They will be plaintext strings.
// Worker code:console.log(API_TOKEN);//=> (default) "example_dev_token"//=> (env.production) "example_production_token"console.log(STRIPE_TOKEN);//=> (default) "pk_xyz1234_test"//=> (env.production) "pk_xyz1234"
Adding secrets via wrangler
Secrets are defined by running in your terminal, where
<NAME> is the name of your binding. You may assign environment-specific secrets by re-running the command
wrangler secret put <NAME> -e or
wrangler secret put <NAME> --env. Keep a list of the secrets used in your code in your
wrangler.toml file, like the example under
Adding KV namespaces via wrangler
Environment variables via the dashboard
Adding environment variables via the dashboard
To add environment variables, such as
- Go to your Workers script > Settings > Add variable under Environment Variables.
- Input a Variable name and its value, which will be made available to your Worker.
- If your variable is a secret, select Encrypt to protect its value. This will prevent the value from being visible via
wranglerand the dashboard.
- (Optional) To add multiple environment variables, select Add variable.
- Select Save to implement your changes.
Adding KV namespace bindings via the dashboard
To add KV namespace bindings:
- Go to your Workers script > Settings > Add binding under KV Namespace Bindings.
- Choose a Variable name. This will be the way the variable name will be referenced in your Worker script.
- Next, select a KV namespace from the dropdown.
- Select Add binding to add multiple bindings.
- When you are finished, select Save to implement your changes.
Your completed Workers dashboard, with environment variables and KV namespace bindings added, will look like the following example reference.
Comparing secrets and environment variables
Secrets are environment variables. The difference is secret values are not visible within
wrangler or dashboard interfaces after you define them. This means that sensitive data, including passwords or API tokens, should always be encrypted to prevent data leaks. To your Worker, there is no difference between an environment variable and a secret. The secret's value is passed through as defined.