Skip to content

Commit 3785ef9

Browse files
committed
update environment variables page
1 parent 9b48f5e commit 3785ef9

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ 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, WranglerConfig } from "~/components";
109

1110
## Background
1211

@@ -26,31 +25,34 @@ 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

50+
### Configuring different environments in Wrangler
51+
52+
You can set up [environments in Wrangler](/workers/wrangler/environments), and specify different values for your environment variables in each environment.
5153
`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.
5254

53-
To define environment variables for different environments, refer to the example below:
55+
The example below sets up two environments, `staging` and `production`, with different values for `API_HOST` and `API_ACCOUNT_ID`.
5456

5557
<WranglerConfig>
5658

@@ -70,14 +72,9 @@ SERVICE_X_DATA = { URL = "service-x-api.prod.example", MY_ID = 456 }
7072

7173
</WranglerConfig>
7274

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.
74-
75-
```shell
76-
API_HOST = "localhost:4000"
77-
API_ACCOUNT_ID = "local_example_user"
78-
```
75+
To run Wrangler commands in specific environments, you can pass in the `--env` or `-e` flag. For example, you can develop the Worker in the `dev` environment by running `npx wrangler dev -e=staging`, and deploy it with `npx wrangler deploy -e=production`.
7976

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`.
77+
Learn about [environments in Wrangler](/workers/wrangler/environments).
8178

8279
## Add environment variables via the dashboard
8380

@@ -94,14 +91,14 @@ To add environment variables via the dashboard:
9491

9592
:::caution[Plaintext strings and secrets]
9693

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

100-
10196
:::
10297

10398
<Render file="env_and_secrets" />
10499

100+
<Render file="secrets-in-dev" />
101+
105102
## Related resources
106103

107-
* Learn how to access environment variables in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
104+
- Migrating environment variables from [Service Worker format to ES modules syntax](/workers/reference/migrate-to-module-workers/#environment-variables).

0 commit comments

Comments
 (0)