Cloudflare changelogs | Developer platform

Cloudflare changelogs for Developer platform products

马上订阅 Cloudflare changelogs | Developer platform RSS 更新: https://developers.cloudflare.com/changelog/rss/developer-platform.xml

Workers - Import `env` to access bindings in your Worker's global scope

2025年3月17日 08:00
Workers

You can now access 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 or environment variables in the initial setup for your Worker:

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
},
};