Environment Variables & Secrets in Workers
Configure your Cloudflare Worker with environment variables and securely manage secrets like API keys.
Environment Variables & Secrets in Workers is a free Edge Computing with Cloudflare Workers & Deno lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Edge Computing with Cloudflare Workers & Deno learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Configuration Matters
Hardcoding values like API keys into your Worker is insecure and inflexible. Environment variables and secrets keep config out of your code.
Vars vs Secrets
Workers distinguish two kinds:
- Vars: plain text config, visible in dashboard
- Secrets: encrypted, write-only values like keys and tokens
Defining Vars in wrangler
Plain variables go in your wrangler.toml under a [vars] table.
# wrangler.toml
[vars]
API_BASE = 'https://api.example.com'
ENVIRONMENT = 'production'Accessing the env Binding
In the modules format, config arrives as the env argument of your fetch handler.
export default {
async fetch(request, env, ctx) {
return new Response('Base: ' + env.API_BASE);
}
};Adding a Secret
Secrets are never stored in your repo. Add them with the Wrangler CLI, which prompts for the value.
wrangler secret put MY_API_KEYUsing a Secret
Secrets appear on the same env object as vars, so reading them is identical.
const res = await fetch('https://api.example.com/data', {
headers: { Authorization: 'Bearer ' + env.MY_API_KEY }
});Listing & Deleting Secrets
Manage secrets entirely from the CLI. You can list names (never values) and delete ones you no longer need.
wrangler secret list
wrangler secret delete MY_API_KEYPer-Environment Config
Use named environments to give staging and production different values.
# wrangler.toml
[env.staging.vars]
ENVIRONMENT = 'staging'Local Development Secrets
For local wrangler dev, put secrets in a .dev.vars file and add it to .gitignore so they never reach version control.
# .dev.vars
MY_API_KEY=local-test-keySecurity Best Practices
Keep config safe:
- Never commit secrets
- Rotate keys periodically
- Use secrets, not vars, for anything sensitive
Putting It Together
A typical Worker reads a public base URL from vars and an auth token from a secret, then calls an upstream API safely.
export default {
async fetch(req, env) {
const r = await fetch(env.API_BASE + '/me', {
headers: { Authorization: 'Bearer ' + env.MY_API_KEY }
});
return new Response(await r.text());
}
};Quick Check
Test your config knowledge.
Recap
You learned the difference between vars and secrets, how to define and access them via the env binding, manage them with Wrangler, and keep sensitive values secure across environments.
Frequently asked questions
Is the “Environment Variables & Secrets in Workers” lesson free?
Yes — the full text of “Environment Variables & Secrets in Workers” is free to read here on the web, and the Edge Computing with Cloudflare Workers & Deno course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Edge Computing with Cloudflare Workers & Deno course, upgrade to CoddyKit PRO.
What will I learn in “Environment Variables & Secrets in Workers”?
Configure your Cloudflare Worker with environment variables and securely manage secrets like API keys. You practise Edge Computing with Cloudflare Workers & Deno with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Edge Computing with Cloudflare Workers & Deno?
No prior experience is required. Edge Computing with Cloudflare Workers & Deno on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Environment Variables & Secrets in Workers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Edge Computing with Cloudflare Workers & Deno lesson?
Yes. Every Edge Computing with Cloudflare Workers & Deno lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up Worker Environment
- Handling HTTP Requests
- Deploying Your First Worker
- Environment Variables & Secrets in Workers