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.
Paste this into your agent
Copied! Now paste it into your agent. No agent yet? Here's how to get one.
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 variableAPI_TOKEN, a secretNOTES, 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
cfCLI 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
envexposes 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
NOTESshows 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.