Skip to content

Commit 4a30651

Browse files
committed
Adds section for managing environment variables for multiple environments, and adds more details to Secrets vs Environment variables
1 parent 69c1c86 commit 4a30651

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

src/content/docs/workers/configuration/environment-variables.mdx

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ pcx_content_type: configuration
33
title: Environment variables
44
head: []
55
description: Environment variables are a type of binding that allow you to attach text strings or JSON values to your Worker
6-
76
---
87

9-
import { Render, TabItem, Tabs, WranglerConfig } from "~/components"
8+
import { Render, TabItem, Tabs, Aside, WranglerConfig } from "~/components";
109

1110
## Background
1211

13-
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/).
1413

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/).
1615

1716
## Add environment variables via Wrangler
1817

@@ -26,31 +25,37 @@ Refer to the following example on how to access the `API_HOST` environment varia
2625

2726
```js
2827
export default {
29-
async fetch(request, env, ctx) {
30-
return new Response(`API host: ${env.API_HOST}`);
31-
}
32-
}
28+
async fetch(request, env, ctx) {
29+
return new Response(`API host: ${env.API_HOST}`);
30+
},
31+
};
3332
```
3433

3534
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
3635

3736
```ts
3837
export interface Env {
39-
API_HOST: string;
38+
API_HOST: string;
4039
}
4140

4241
export default {
43-
async fetch(request, env, ctx): Promise<Response> {
44-
return new Response(`API host: ${env.API_HOST}`);
45-
}
42+
async fetch(request, env, ctx): Promise<Response> {
43+
return new Response(`API host: ${env.API_HOST}`);
44+
},
4645
} satisfies ExportedHandler<Env>;
4746
```
4847

4948
</TabItem> </Tabs>
5049

51-
`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+
<Aside type="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
5257

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`).
5459

5560
<WranglerConfig>
5661

@@ -69,15 +74,19 @@ SERVICE_X_DATA = { URL = "service-x-api.prod.example", MY_ID = 456 }
6974
```
7075

7176
</WranglerConfig>
72-
73-
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.
7479

7580
```shell
7681
API_HOST = "localhost:4000"
7782
API_ACCOUNT_ID = "local_example_user"
7883
```
7984

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+
```
8190

8291
## Add environment variables via the dashboard
8392

@@ -89,19 +98,17 @@ To add environment variables via the dashboard:
8998
4. Select **Settings**.
9099
5. Under **Variables and Secrets**, select **Add**.
91100
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**.
93102
8. Select **Deploy** to implement your changes.
94103

95104
:::caution[Plaintext strings and secrets]
96105

97-
98106
Select the **Secret** type if your environment variable is a [secret](/workers/configuration/secrets/).
99107

100-
101108
:::
102109

103110
<Render file="env_and_secrets" />
104111

105112
## Related resources
106113

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.
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
---
22
{}
3-
43
---
54

6-
## Compare secrets and environment variables
5+
## Secrets vs Environment Variables
76

87
:::caution[Use secrets for sensitive information]
98

9+
**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/)
1013

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:
1215

16+
```sh
17+
wrangler secret put <KEY>
18+
```
1319

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)
1529

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 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.

0 commit comments

Comments
 (0)