-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Adds documentation for importable env #20509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/content/changelog/workers/2025-03-14-importable-env.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| --- | ||
| title: Import `env` for direct access to bindings in your Worker code | ||
| description: More easily configure your Worker and call bindings from anywhere with an importable `env` | ||
| products: | ||
| - workers | ||
| date: 2025-03-14T15:00:00Z | ||
| --- | ||
|
|
||
| import { Render, TypeScriptExample } from "~/components"; | ||
|
|
||
| You can now access [bindings](/workers/runtime-apis/bindings/) | ||
| from anywhere in your Worker by importing the `env` object from `cloudflare:workers`. | ||
|
|
||
| Previously, `env` could only be accessed during a request. This meant that | ||
| bindings could not be used in the top-level context of a Worker. | ||
|
|
||
| Now, you can import `env` and access bindings such as [secrets](/workers/configuration/secrets/) | ||
| or [environment variables](/workers/configuration/environment-variables/) in the | ||
| initial setup for your Worker: | ||
|
|
||
| ```js | ||
| import { env } from "cloudflare:workers"; | ||
| import ApiClient from "example-api-client"; | ||
|
|
||
| // API_KEY and LOG_LEVEL now usable in top-level scope | ||
| const apiClient = ApiClient.new({ apiKey: env.API_KEY }); | ||
| const LOG_LEVEL = env.LOG_LEVEL || "info"; | ||
|
|
||
| export default { | ||
| fetch(req) { | ||
| // you can use apiClient or LOG_LEVEL, configured before any request is handled | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| :::note | ||
| Workers do not allow I/O from outside a request context. This means that even | ||
| though `env` is accessible from the top-level scope, you will not be able to access | ||
| every binding's methods. | ||
|
|
||
| For instance, environment variables and secrets are accessible, and you are able to | ||
| call `env.NAMESPACE.get` to get a [Durable Object stub](/durable-objects/api/stub/) in the | ||
| top-level context. However, calling methods on the Durable Object stub, making [calls to a KV store](/kv/api/), | ||
| and [calling to other Workers](/workers/runtime-apis/bindings/service-bindings) will not work. | ||
| ::: | ||
|
|
||
| Additionally, `env` was normally accessed as a argument to a Worker's entrypoint handler, | ||
| such as [`fetch`](/workers/runtime-apis/fetch/). | ||
| This meant that if you needed to access a binding from a deeply nested function, | ||
| you had to pass `env` as an argument through many functions to get it to the | ||
| right spot. This could be cumbersome in complex codebases. | ||
|
|
||
| Now, you can access the bindings from anywhere in your codebase | ||
| without passing `env` as an argument: | ||
|
|
||
| ```js | ||
| // helpers.js | ||
| import { env } from "cloudflare:workers"; | ||
|
|
||
| // env is *not* an argument to this function | ||
| export async function getValue(key) { | ||
| let prefix = env.KV_PREFIX; | ||
| return await env.KV.get(`${prefix}-${key}`); | ||
| } | ||
| ``` | ||
|
|
||
| For more information, see [documentation on accessing `env`](/workers/runtime-apis/bindings#how-to-access-env). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.