You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Environment variables are a type of binding that allow you to attach text strings or JSON values to your Worker. Environment variables are available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters)passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/).
12
+
Environment variables are a type of binding that allow you to attach text strings or JSON values to your Worker. They are pass in via the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters)in your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/).
14
13
15
-
Text strings and JSON values are not encrypted and are useful for storing application configuration.
14
+
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](/workers/configuration/secrets/).
16
15
17
16
## Add environment variables via Wrangler
18
17
@@ -26,31 +25,37 @@ Refer to the following example on how to access the `API_HOST` environment varia
`vars` is a non-inheritable key. [Non-inheritable keys](/workers/wrangler/configuration/#non-inheritable-keys) are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.
50
+
<Asidetype="note">
51
+
52
+
**`vars` is a non-inheritable key**. [Non-inheritable keys](/workers/wrangler/configuration/#non-inheritable-keys) are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.
53
+
54
+
</Aside>
55
+
56
+
## Managing environment variables across multiple environments
52
57
53
-
To define environment variables for different environments, refer to the example below:
58
+
Since `vars` is a [non-inheritable key](/workers/wrangler/configuration/#non-inheritable-keys), you need to explicitly define your environment variables for each environment (for example, `staging` and `production`).
For local development with `wrangler dev`, variables in the [Wrangler configuration file](/workers/wrangler/configuration/) 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.
77
+
### Overriding environment variables during local development
78
+
When running [`wrangler dev`](/workers/wrangler/commands/#dev), variables in the [Wrangler configuration file](/workers/wrangler/configuration/) 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.
74
79
75
80
```shell
76
81
API_HOST = "localhost:4000"
77
82
API_ACCOUNT_ID = "local_example_user"
78
83
```
79
84
80
-
Alternatively, you can specify per-environment values in the [Wrangler configuration file](/workers/wrangler/configuration/) and provide an `environment` value via the `env` flag when developing locally like so `wrangler dev --env=local`.
85
+
Alternatively, you can specify per-environment values in the [Wrangler configuration file](/workers/wrangler/configuration/) and provide an `environment` value via the `--env` flag when developing locally:
86
+
87
+
```sh
88
+
wrangler dev --env=local
89
+
```
81
90
82
91
## Add environment variables via the dashboard
83
92
@@ -89,19 +98,17 @@ To add environment variables via the dashboard:
89
98
4. Select **Settings**.
90
99
5. Under **Variables and Secrets**, select **Add**.
91
100
6. Select a **Type**, input a **Variable name**, and input its **Value**. This variable will be made available to your Worker.
92
-
7. (Optional) To add multiple environment variables, select **Add variable**.
101
+
7. (_Optional_) To add multiple environment variables, select **Add variable**.
93
102
8. Select **Deploy** to implement your changes.
94
103
95
104
:::caution[Plaintext strings and secrets]
96
105
97
-
98
106
Select the **Secret** type if your environment variable is a [secret](/workers/configuration/secrets/).
99
107
100
-
101
108
:::
102
109
103
110
<Renderfile="env_and_secrets" />
104
111
105
112
## Related resources
106
113
107
-
* Learn how to access environment variables in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
114
+
- Learn how to access environment variables in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
**Never store sensitive information in plaintext environment variables**. Always use [secrets](/workers/configuration/secrets/) for data like passwords or API tokens.
10
+
:::
11
+
12
+
### When to use [secrets](/workers/configuration/secrets/)
10
13
11
-
Do not use plaintext environment variables to store sensitive information. Use [secrets](/workers/configuration/secrets/) instead.
14
+
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](/workers/configuration/secrets/#secrets-on-deployed-workers) command:
12
15
16
+
```sh
17
+
wrangler secret put <KEY>
18
+
```
13
19
14
-
:::
20
+
Secrets function similarly to environment variables in a Worker, but with crucial differences:
21
+
22
+
-**Visibility:** Once you define a secret, its value is no longer visible in Wrangler or the Cloudflare dashboard.
23
+
24
+
-**Security:** Sensitive data, such as passwords and tokens, should always be encrypted to prevent accidental exposure.
25
+
26
+
To your Worker, **there is no difference between an environment variable and a secret.** The secret's value is passed through as defined.
27
+
28
+
### When to use [plaintext environment variables](/workers/configuration/environment-variables)
15
29
16
-
[Secrets](/workers/configuration/secrets/) are [environment variables](/workers/configuration/environment-variables/). The difference is secret values are not visible within Wrangler or Cloudflare dashboard 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.
30
+
Plaintext environment variablesare 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.
0 commit comments