Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

Worker Bindings, env Access, Configuration, and Local Development

What Worker bindings are, how to declare them in Wrangler, read them via env, use them in frameworks, and develop locally without leaking secrets.

This page summarises Cloudflare documentation on bindings: typed links between a Worker (or Pages Function) and resources on the Cloudflare Developer Platform—KV, R2, D1, Queues, Durable Objects, Workers AI, and others—without embedding API tokens in your code.

Bindings (env): what they are and why to use them

Section titled “Bindings (env): what they are and why to use them”

A binding is both a permission and an API: you declare it in configuration, then call it on the env object at runtime. Cloudflare states:

You can think of a binding as a permission and an API in one piece. With bindings, you never have to add secret keys or tokens to your Worker in order to access resources on your Cloudflare account — the permission is embedded within the API itself. The underlying secret is never exposed to your Worker’s code, and therefore can’t be accidentally leaked.

— Cloudflare, Bindings (env)

Compared with calling account REST APIs from inside a Worker, bindings are in-process references with no extra network hop and no separate authentication step. The Workers best practices guide recommends bindings for services such as R2, KV, D1, Queues, and Workflows rather than REST from Worker code.

During local development, bindings usually connect to locally simulated resources; you can also use remote bindings to hit real account resources. See Bindings (env).

Which platform resources support bindings today

Section titled “Which platform resources support bindings today”

The runtime overview lists binding types currently available, including AI, D1, Durable Objects, Hyperdrive, KV, Queues, R2, Secrets, service bindings, Stream, Vectorize, Workflows, Assets, and more. The full list lives in Bindings (env). Product-specific pages (for example Stream bindings or Pages Functions bindings) may describe a subset of what a given runtime supports.

Runtime Where bindings are set Typical access pattern
Cloudflare Workers Wrangler config, dashboard, Terraform, upload metadata env.BINDING_NAME in fetch or cloudflare:workers
Pages Functions Dashboard (deployed); Wrangler for local context.env / EnvWithFetch
Workers for Platforms (user Workers) Upload API metadata env on each user Worker invocation
Framework on Workers (Astro, Nuxt, etc.) Wrangler + framework adapter locals.runtime.env, context.cloudflare.env, or env import

Wrangler configuration: how to declare a binding

Section titled “Wrangler configuration: how to declare a binding”

Bindings are declared in the Wrangler configuration file (JSON or TOML). A minimal R2 example from the runtime docs:

{
"main": "./src/index.js",
"r2_buckets": [
{
"binding": "MY_BUCKET",
"bucket_name": "<MY_BUCKET_NAME>"
}
]
}

The binding name MY_BUCKET becomes env.MY_BUCKET in code. The same pattern applies to KV namespaces, D1 databases, queues, secrets, and other types; see Bindings (env) and the binding list in Multipart upload metadata.

For Durable Object bindings consumed via getPlatformProxy, Cloudflare requires a script_name when the object class lives in another Worker—for example binding MyDurableObject to class MyDurableObject on script external-do-worker. Details and multi-config wrangler dev examples are in Wrangler API — Supported bindings.

When attaching bindings to user Workers through the dispatch API, metadata includes a bindings array (KV, R2, etc.). Use keep_bindings when adding bindings so existing ones are not dropped—see Workers for Platforms configuration — Bindings.

In Terraform, bindings use a single bindings array with a type field and type-specific properties (for example plain_text, kv_namespace, secret_text), not separate top-level Wrangler keys. See Infrastructure as Code — Bindings in Terraform.

Bindings are exposed on env, which you reach in several equivalent ways:

  1. Handler argument — async fetch(request, env) { … }
  2. Class property — this.env on WorkerEntrypoint, DurableObject, or Workflow classes
  3. Module import — import { env } from "cloudflare:workers" (useful for nested helpers; see limits below)

Python Workers use self.env on WorkerEntrypoint, as shown in Foreign Function Interface (FFI).

cloudflare:workers env import: when it works

Section titled “cloudflare:workers env import: when it works”

Importing env at module top level is supported for configuration-like bindings (vars, secrets) and for obtaining stubs (for example a Durable Object namespace). I/O on bindings—KV get, calls on DO stubs, service bindings—must run inside a request context:

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.

— Cloudflare, Bindings (env)

Passing env as an argument remains a good pattern for testing and dependency injection, as noted in the same section.

Pages Functions and framework applications

Section titled “Pages Functions and framework applications”

Pages Functions use bindings to talk to KV, D1, R2, Durable Objects, and related products; production and preview environments can differ. Configuration for deployed sites is done in the dashboard; Wrangler config is central for local work. See Pages Functions bindings and Wrangler configuration for Pages Functions.

EnvWithFetch on Pages holds vars, secrets, bindings, and the ASSETS binding for static fallback—Pages Functions API reference.

Framework-specific entry points (server-only):

Framework How bindings are reached
Astro (SSR on Cloudflare) locals.runtime.env via middleware; types via wrangler types — Deploy an Astro site
Astro on Workers Bindings unavailable for purely static output — Astro on Workers
Nuxt context.cloudflare.env in server routes; local dev via nitro-cloudflare-dev / getPlatformProxy — Deploy a Nuxt site
React Router on Workers context.cloudflare.env in loaders/actions — React Router
TanStack Start import { env } from "cloudflare:workers" in server functions — TanStack Start
Next.js (vinext on Workers) cloudflare:workers in server components, handlers, actions — Next.js
QwikCity platform.env in route loaders — Deploy a Qwik site
Vue / React SPA Front-end calls a colocated Worker (./server/index.ts or ./worker/index.ts) that holds bindings — Vue, React + Vite

Analog, Hono, Solid, SvelteKit, Waku, Vike, and Docusaurus follow the same model: configure in Wrangler, integrate via each framework’s Cloudflare adapter docs linked from Workers framework guides.

Local development: getPlatformProxy and remote bindings

Section titled “Local development: getPlatformProxy and remote bindings”

For Node-based tooling (tests, Nitro, framework dev servers), getPlatformProxy from the wrangler package reads the Wrangler config and exposes bindings on env:

import { getPlatformProxy } from "wrangler";
const { env } = await getPlatformProxy();
console.log(`MY_VARIABLE = ${env.MY_VARIABLE}`);

Documented options include environment, configPath, persist (align paths with wrangler dev --persist-to by appending /v3 when sharing storage), and remoteBindings (default true). See Wrangler API.

Important limits from the same page:

  • getPlatformProxy does not execute your Worker script; it ignores Workflows declared in Wrangler exports.
  • Workers AI always uses your Cloudflare account and incurs charges even locally.
  • Hyperdrive binding fields may be passthrough values meaningful only inside workerd, not in Node.

For Pages Functions local dev, attach bindings per Local development and the binding guide for each resource.

Miniflare (unit tests) does not read Wrangler config; pass bindings, kvNamespaces, etc. in the Miniflare constructor, or use getBindings() in tests—Writing tests, Variables and secrets.

  • Calling REST APIs from Worker code when a binding exists for the same product (Workers best practices).
  • Expecting KV I/O at module top level via import { env } from "cloudflare:workers".
  • Caching secrets-derived clients in global scope after binding-only deploys.
  • Inherit binding failures (error 10057) when upload metadata references a binding name missing from the inherited version, or when version_id / bindings_inherit=strict rules are violated—Errors and exceptions.
  • Forgetting script_name on Durable Object bindings that point at another Worker script when using getPlatformProxy.
  1. List bindings in your Wrangler file and compare them to the types in Bindings (env) and, for Pages, Pages Functions bindings.

  2. Run wrangler types (or your framework’s cf-typegen script) so TypeScript matches configured binding names, as described for Astro and TanStack Start.

  3. Locally, log a plain-text var via getPlatformProxy() following Wrangler API — Usage, or exercise KV/R2 through Miniflare getBindings() per Writing tests.

  4. Deploy, then confirm production and preview bindings in the Cloudflare dashboard if you use Pages—Pages Functions bindings.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match your project or the linked Cloudflare docs, email yimasynthetic@gmail.com with the page URL and the exact wording you think is wrong. We will clarify or correct it.