IntermediateClaude Academy

Environment variables that work everywhere

Everything works on your computer. Then you open Claude Code in the cloud, or deploy your site, and none of your keys work. Nothing is broken: each place keeps its own copy of your settings, and the cloud was never given one. This guide explains every term from zero and gives you a routine that keeps them all in sync.

Time: 25 minYou need: a project in Claude Code that lives on GitHub. Vercel and Supabase only if you use them.

A laptop and a cloud, each holding its own copy of the same environment variable names
Watch the whole idea in 50 seconds, then follow the steps below.

Understand it

Know what an environment variable is

An environment variable is a named setting that lives outside your code. The name stays the same wherever the code runs, for example STRIPE_SECRET_KEY. The value can change from place to place: a test key on your computer, the live key on your real website.

A .env file (say "dot env") is a plain text file in your project folder that holds those name and value pairs. When your app starts, it reads the file and your code asks for each value by its name.

  • Each line is one pair: the name, an equals sign, then the value. No spaces around the =.
  • Names are written in capitals with underscores, like SITE_NAME.
  • A line that starts with # is a note for humans. The app ignores it.
Diagram: a .env file with three name and value lines, and your code asking for SITE_NAME and getting back My Shop
Your code never holds the key itself. It asks for it by name.
Can't see your .env file? (Mac)

Files that start with a dot are hidden by default. Open your project folder in Finder and press Command + Shift + . (period) to show them. Press it again to hide them.

WHAT YOU SHOULD SEEIf your project already has a .env, it is a short list of lines shaped like NAME=value. If it has none yet, that is fine: step 6 makes one.

Understand it

Keep .env out of GitHub

Your .env holds real keys, so it must never be uploaded to GitHub, even in a private repo. A file called .gitignore tells git which files to leave behind. Two lines cover it.

.gitignore
.env*
!.env.example

The first line skips every file whose name starts with .env, such as .env and .env.local. The ! on the second line makes one exception, so the list from step 3 still gets shared.

  • Ask Claude: "Check that my .gitignore keeps every .env file out of git, and tell me if a .env was ever committed. Don't show me any values."
  • If the lines are missing, let Claude add them before you do anything else with your keys.
Diagram: src, CLAUDE.md and .env.example go from your computer to GitHub, while .gitignore stops .env at the door
Everything goes to GitHub except the file with your keys.
GUARDRAILIf a real secret key was ever committed, deleting the file does not fix it. Old versions stay in the project's history. Go to the service that issued it (Stripe, Supabase and so on), create a new key, swap it in everywhere and delete the old one. That is called rotating the key.

Understand it

Make a .env.example

Your .env stays on your computer, so how does anyone else (a new laptop, a cloud session, Claude itself) know which settings the project needs? With a second file, .env.example: the same names, with the values left blank. It is safe to share, so it goes to GitHub with your code.

  • Ask Claude: "Make a .env.example from my .env. Keep every name, leave every value blank, and add a short comment above each one saying what it is for and where to get it."
  • Read it and check no real value slipped in.
  • Treat it as the official list. Whenever you set up the project somewhere new, you work through it top to bottom.
.env.example
# Public: your Supabase project URL, from your project's settings
VITE_SUPABASE_URL=

# Public: the Supabase publishable (or anon) key, safe in the browser
VITE_SUPABASE_PUBLISHABLE_KEY=

# Secret: Stripe secret key. Server only. Use sk_test_ outside production.
STRIPE_SECRET_KEY=

These names are examples. Keep whatever names your project already uses.

Diagram: .env.example has the names with blank values and goes to GitHub; .env has the same names with real values and stays on your computer
The menu and the order: one lists what exists, the other holds what you picked.
GUARDRAILNever put a real value in .env.example. It is uploaded on purpose, so anything in it is shared.

Understand it

Tell public keys from secret keys

Not everything in your .env is a secret. Some values are meant to reach the browser. Any name that starts with VITE_ or NEXT_PUBLIC_ gets built into the page code that every visitor downloads. That is by design for publishable keys, like Stripe's pk_ key or Supabase's publishable (anon) key, which can only do what your settings allow.

Secret keys are different. A Stripe secret key or a Supabase secret (service_role) key can read, change or charge almost anything. They belong only on a server.

  • Public keys are fine to show Claude, paste in chat, or leave visible in a screenshot.
  • Secret keys go straight into the file or dashboard, typed by you. Not into the chat.
  • Never give a secret key a VITE_ or NEXT_PUBLIC_ name.
Two columns: public values like VITE_ and NEXT_PUBLIC_ names and publishable keys, and secret values like Stripe and Supabase secret keys and webhook signing secrets
When you're not sure which column a key belongs in, treat it as secret.
GUARDRAILA secret key with a VITE_ or NEXT_PUBLIC_ name stops being secret. Anyone can open your site's code in their browser and copy it. If that has happened, rotate the key (see step 2).

Give each place its copy

See why the cloud can't find your keys

This is where most people get stuck. A Claude Code cloud session (one you start at claude.ai/code, on your phone, or under Cloud in the desktop app) runs on a computer Anthropic manages, not yours. It starts from a fresh copy of your project on GitHub. Your .env was never on GitHub, because step 2 kept it off, so the cloud starts without it. Even when Claude Code uploads your folder directly instead of using GitHub, it leaves .env files behind on purpose.

Your live website works the same way. Vercel and Supabase build from GitHub too. So every place that runs your code needs its own copy of the values:

  • Your computer: the .env file.
  • Claude Code in the cloud: the environment variables on your cloud environment.
  • Vercel (your website): the project's Environment Variables page.
  • Supabase (your backend functions): Edge Function Secrets.
Map: your computer pushes code to GitHub; the Claude cloud, Vercel and Supabase each copy the code from GitHub, and each keeps its own values
The code is shared. The values are not.
WHAT YOU SHOULD SEEIn a cloud session, ask: "Which variables from .env.example are set in this session? List names only, no values." Anything on the missing list is what steps 7 to 9 fix.

Give each place its copy

Fill in your local .env

Start at home. Your computer's copy is the .env file in the project folder, built from the list in .env.example.

  • Ask Claude: "Create .env from .env.example with every value left blank. I'll paste the values in myself."
  • Open each service's dashboard, copy the key, and paste it after the =. No quotes, no spaces.
  • Save the file, then stop and restart your app so it reads the new values.
A project folder showing .env with real values, .env.example as the committed list, .gitignore, CLAUDE.md and src, plus a reminder to restart the dev server after editing .env
One project, one .env. Your other projects each get their own.
GUARDRAILValues belong to one project. Two projects can both have SUPABASE_URL and point at completely different databases. Never copy a value across because the name matches. Look it up in that project's own dashboard.

Give each place its copy

Add them to your Claude Code cloud environment

Every cloud session runs inside a cloud environment: a saved setup that controls what the session can reach on the internet, a setup script, and its environment variables. Your first one is called Default and starts with no variables at all. That is why the cloud felt empty.

  • At claude.ai/code, on the screen where you start a new session, open the environment selector. It is the first control in the row under the message box.
  • Pick New environment at the bottom of the menu, or hover over an existing one under Cloud and select the settings icon on its right. In the desktop app it is the same selector: New environment, then the Cloud tab, or the gear icon to edit.
  • Select the Environment variables row and paste your NAME=value lines, in the same format as your .env. Put quotes around any value that contains a #.
  • Save with Add environment or Save changes, then start a new session. A session that is already running keeps the old values until it restarts.

Make one environment per project and name it after the project, so one project's values never end up in another.

Diagram: a cloud session holds a fresh copy of the repo and the environment variables; network secrets stay outside and are added to requests for the websites you list
Environment variables live inside the session. Network secrets never do.
GUARDRAILAnyone who uses the environment can read its variables, and the dialog warns you not to put secrets there. For an API key that Claude only needs to call a service, use the Network secrets row instead. Select Add secret, list the websites it is for, and paste the key. It stays outside the session and is added to those requests on the way out, so Claude never sees it. You can't view it again after saving. If a key really must sit in a variable, use a test key, not your live one.

Give each place its copy

Add them to Vercel

If your site is hosted on Vercel, it builds from GitHub too, so it needs its own copy. Vercel lets one variable hold a different value for each kind of deployment.

  • Open your project in the Vercel dashboard and select Environment Variables in the sidebar.
  • Enter the Name and Value, then pick where it applies: Production (your main branch, the live site), Preview (every other branch and pull request) or Development (for your computer).
  • Mark keys as Secret rather than Config. Vercel then won't show the value again after you save it.
  • Click Save, then redeploy. Changes only reach new deployments.
Diagram: one variable, STRIPE_SECRET_KEY, with a live key for Production and test keys for Preview and Development
Live keys only where real customers are.
Shortcut: pull Vercel's values down to your computer

If you use the Vercel command line tool, vercel env pull downloads the Development values into a .env file in your project. It writes the file for you, so check it didn't replace anything you only had locally.

WHAT YOU SHOULD SEEAfter the redeploy finishes, the feature that used the key works on your live site.

Give each place its copy

Add them to Supabase

Supabase edge functions are small bits of backend code that run on Supabase's servers. This is where secret keys like Stripe's usually live, because the browser never sees them. Edge functions read their own list, called Edge Function Secrets.

  • On the dashboard, open your project's Edge Function Secrets page, enter a Key and Value, and click Save. You can paste several at once.
  • Or let Claude do it from your computer with the Supabase command line tool: supabase secrets set --env-file supabase/functions/.env. Then supabase secrets list shows the names that are set.
  • No redeploy needed. Your functions pick up new secrets right away.
Diagram: secrets go from supabase/functions/.env on your computer to Edge Function Secrets on Supabase, where your edge functions read them right away
The local copy for functions lives in supabase/functions/.env, which the .env* line already keeps out of git.
GUARDRAILSupabase already gives every function SUPABASE_URL and the project's own keys, so don't add those yourself. Your own names can't start with SUPABASE_. The secret (service_role) key skips your database's security rules, so it belongs here and nowhere near a VITE_ or NEXT_PUBLIC_ name.

Keep them in sync

Follow one routine for every new key

Things drift out of sync one key at a time. You add a new service locally, it works, and three weeks later the cloud session or the live site breaks. Run the same loop every time a key is added, changed or deleted.

  • Add the name to .env.example with a comment, and commit it with the change that uses it.
  • Put the value in your local .env.
  • Add it to each place that runs code needing it: your cloud environment, Vercel (the right environments) and Supabase.
  • Redeploy Vercel, or start a new cloud session, then note it in CLAUDE.md (next step).
Five numbered steps: add the name to .env.example, put the value in your .env, add it to cloud, Vercel and Supabase, redeploy or start a new session, note it in CLAUDE.md
Rotated a key? Same loop: every place that had the old one gets the new one.
WHAT YOU SHOULD SEEAsk Claude: "I just added a new variable. Walk me through every place it needs a value." It should name each place from the list above.

Keep them in sync

Write the rules into CLAUDE.md

CLAUDE.md is the file Claude reads at the start of every session in your project. Put your variable list and your rules there once, and Claude follows them in every session, on your computer and in the cloud, without you repeating yourself.

  • Paste the snippet below into your project's CLAUDE.md.
  • Swap the example rows for your own variables. Ask Claude to fill the table from .env.example if you like.
  • Commit it, so cloud sessions get it too.
CLAUDE.md
## Environment

.env.example is the list of every variable this project needs.

| Variable                      | Type   | Lives in                          |
|-------------------------------|--------|-----------------------------------|
| VITE_SUPABASE_URL             | public | .env, cloud environment, Vercel   |
| VITE_SUPABASE_PUBLISHABLE_KEY | public | .env, cloud environment, Vercel   |
| STRIPE_SECRET_KEY             | secret | Supabase Edge Function Secrets    |

Rules:
- Values belong to this project only. Never copy one from another project,
  even when the name matches.
- Never print, log or commit a secret value. Refer to secrets by name.
- Public values (VITE_, NEXT_PUBLIC_, publishable keys) are fine to show.
- Never give a secret key a VITE_ or NEXT_PUBLIC_ name.
- When you add a variable, add its name to .env.example in the same change
  and tell me every place that needs a value.
- If .env is missing, offer to create it from .env.example with blank
  values. Never make up values.
Example CLAUDE.md Environment section with a table of variables, each marked public or secret, and where it lives
An example. Your table lists your own variables.
WHAT YOU SHOULD SEEStart a fresh cloud session and ask: "Check which variables from .env.example are missing here, names only." The answer is an empty list, or a short one you know how to fix.

Get the free setup checklist

Every step from this guide on two pages: the one-time setup, where each copy of your keys lives, the routine for new keys, and the CLAUDE.md section ready to paste.

  • Setup checklist2 pages: one-time setup, where each copy of your keys lives, routine for new keys.
  • CLAUDE.md sectionAn Environment section you can paste into your project.
Get the free checklist →