Bindings · Exercise 01

Live objects

Give your Worker a plain variable, a secret, and a KV binding, then see for yourself which of them is a string and which is a live object.

Live objects

Most platforms hand your code strings: a database URL, an API key, a token. Your code turns them into connections. On Cloudflare, your Worker’s env can hold strings too, but its bindings are something else: objects that are already connected to a resource, with the permission built in.

In this exercise you’ll put both kinds side by side and look at them.

What you’ll build

Your Worker gets three new entries on env:

  • GREETING, a plain text variable
  • API_TOKEN, a secret
  • NOTES, a Workers KV binding, declared without creating the namespace first

You’ll deploy it, let cf create the namespace for you, and then ask your Worker what each entry actually is. One of them turns out to have nothing in it to leak.

Prerequisites

Your agent can help you with these.

  • A Worker with a cloudflare.config.ts, like the one from Deploy a Worker. Your agent can make a new one if you don’t have it.
  • The cf CLI logged in to your Cloudflare account (cf auth whoami).

Strings vs. bindings

On a typical platform, code that reads from a database gets a URL and a key, and builds a client from them:

// A typical platform (not Cloudflare)
const db = connect(env.DATABASE_URL, env.DATABASE_KEY);
const value = await db.get("content");

On Cloudflare, you declare the binding in cloudflare.config.ts:

cloudflare.config.tsenv: {
  GREETING: bindings.text("Hello"),
  API_TOKEN: bindings.secret(),
  NOTES: bindings.kv(),
},

Then env.NOTES arrives already connected. There’s nothing to set up:

src/index.tsimport { env } from "cloudflare:workers";

export default {
  async fetch(request) {
    return new Response(await env.NOTES.get("content"));
  },
};

GREETING and API_TOKEN are plain strings. NOTES is a live object. Ask your Worker to serialize all three and you get something like this:

GREETING   string   "Hello"
API_TOKEN  string   "pretend-this-is-secret"
NOTES      object   {}

Why it matters

  • A secret is a string, and strings leak. A debug log line or an error reporter that prints env exposes it, and anyone holding that string has the access it grants.
  • A binding has nothing in it to leak. The KV namespace’s real credentials never reach your code, which is why NOTES shows up as {}.
  • Declaring the binding is the whole permission step. There’s no access list to update on the namespace, and a Worker with no binding has no access at all.
  • Cloudflare knows every binding, so it can tell you which Workers use a namespace without reading anyone’s code.