Skip to the workspace
Pebellslearn by building
Workers and Workers KV  ›  Lesson

Bindings — How a Worker Reaches Everything Else

Bindings 12 minA binding is a named resource declared in the Wrangler config and handed to the Worker on env, so code reaches storage, services and secrets without hard-coded credentials.

cloudflare / Bindings — How a Worker Reaches Everything Else

Explore each scene at your own pace.

01 · The rule

The rule

Bindings — How a Worker Reaches Everything Else

Hoplink needs a place to store its links. In a traditional app you would paste a database URL and a password into an environment file, create a client, and hope the secret never leaks. Workers use a different idea: you declare what the Worker is allowed to use, and the runtime hands it over, ready to go.

The rule

Declare it in config; receive it on env; never hard-code credentials.

A binding is a named connection from your Worker to another resource: a KV namespace, an R2 bucket, a D1 database, a Durable Object class, a queue, another Worker, a secret or a plain variable. You declare bindings in the Wrangler configuration file, wrangler.jsonc (older projects use wrangler.toml):

{
  "name": "hoplink",
  "main": "src/index.js",
  "compatibility_date": "2026-09-01",
  "kv_namespaces": [
    { "binding": "LINKS", "id": "<namespace id>" }
  ],
  "vars": { "SITE_NAME": "Hoplink" }
}

At runtime the same name appears as a property of env:

export default {
  async fetch(request, env, ctx) {
    const target = await env.LINKS.get("cat");
    return new Response(`${env.SITE_NAME}: ${target}`);
  },
};

Notice what is missing: no connection string, no API key, no client library. The binding is the permission. A Worker can only touch resources that appear in its config, which makes mistakes and leaks far harder.

Secrets work the same way but are stored encrypted rather than in the file. You add one with wrangler secret put ADMIN_TOKEN, and it arrives as env.ADMIN_TOKEN. Running wrangler types generates a TypeScript Env interface from your config, so your editor knows every binding.

A service binding connects one Worker to another. Hoplink's main Worker could call a separate hoplink-auth Worker through env.AUTH.fetch(request) — a direct call inside Cloudflare, not a trip over the public internet.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.