Environment variables explained (and how to stop leaking API keys)
Updated · 7 min read · by AlgoCrew
Your AI assistant told you to "add your API key to the .env file", and the app worked. But where does that key actually go, who can see it, and why does the app break the moment you deploy? This guide explains environment variables in plain English, then shows the one mistake that quietly exposes your keys to anyone who opens your site.
What an environment variable is
An environment variable is a named setting that lives outside your code. Instead of writing your Stripe key straight into a file, your code says "use whatever STRIPE_SECRET_KEY is set to", and the value is supplied by the place the app runs.
That gives you two things:
- Secrets stay out of your code. You can share or publish the code without sharing the keys.
- Different values per place. Your laptop can use a test database while the live site uses the real one, with no code changes.
The .env file
On your own computer, those values usually live in a file called .env in the root of your project:
DATABASE_URL=postgres://user:password@localhost:5432/myapp
STRIPE_SECRET_KEY=sk_test_51Habc...
OPENAI_API_KEY=sk-proj-abc...
When the app starts, the framework (Next.js, Vite, Node and others) reads this file and makes each line available to your code, for example as process.env.STRIPE_SECRET_KEY.
Three rules for the file itself:
- Never commit it. Make sure
.env(and.env.local) is listed in your.gitignorefile. Most project templates already do this, but check, because AI tools sometimes create new files without updating it. - Commit a
.env.exampleinstead. Same variable names, empty values. It tells anyone (including future you, or a developer you hire) which settings the app needs. - No spaces around
=, and no quotes unless the value contains spaces.
The mistake that leaks your keys: "public" prefixes
This is the part most AI-built apps get wrong.
Frameworks split variables into two kinds: ones only your server can read, and ones that get copied into the JavaScript sent to every visitor's browser. You choose which kind by the name's prefix:
| Framework | Prefix that makes a variable public |
|---|---|
| Next.js | NEXT_PUBLIC_ |
| Vite (incl. many Lovable and Bolt projects) | VITE_ |
| Create React App | REACT_APP_ |
| Expo / React Native | EXPO_PUBLIC_ |
Anything with that prefix is not a secret any more. It is embedded in the files your site sends out, and anyone can find it with the browser's developer tools in under a minute.
When an AI assistant hits an error like "the key is undefined in the browser", a common "fix" is to rename OPENAI_API_KEY to VITE_OPENAI_API_KEY. The error disappears, and your key is now public. People run bots that scan sites for exactly this, and a leaked AI or payment key can run up a real bill.
Which keys are safe to make public
| Key | Public OK? |
|---|---|
Stripe publishable key (pk_live_…, pk_test_…) |
Yes, it is designed for the browser |
Stripe secret key (sk_live_…) |
Never |
| Supabase anon / publishable key | Yes, if Row Level Security is turned on for your tables |
| Supabase service_role / secret key | Never, it bypasses all security |
| OpenAI, Anthropic and other AI API keys | Never |
Database connection strings (DATABASE_URL) |
Never |
| Google Analytics / AdSense IDs | Yes |
The right fix when the browser needs a secret
The browser should never hold the secret. Instead, add a small server-side piece (a Next.js Route Handler, a Supabase Edge Function, a Cloudflare Worker, or a Netlify/Vercel function) that holds the key and makes the call for you. The browser calls your endpoint, and your endpoint calls the AI or payment API.
A good prompt for your AI tool: "Move the OpenAI call into a server-side API route so the API key is only read on the server. Do not use a public env prefix for it."
Why it works locally but breaks when deployed
Your .env file is (correctly) not uploaded with your code, so your hosting provider has no idea what the values are. You have to enter them in its dashboard:
- Vercel: Project → Settings → Environment Variables
- Netlify: Site configuration → Environment variables
- Cloudflare: Workers & Pages → your project → Settings → Variables and Secrets
Two things catch people out:
- You must redeploy after adding or changing a variable. Public (prefixed) values are baked in when the site is built, so the old build keeps the old values.
- Names must match exactly, including the prefix and capital letters.
Check your own site in two minutes
- Open your live site, press
F12(or right-click → Inspect), and open the Sources or Network tab. - Search all files (
Ctrl+Shift+F/Cmd+Option+F) forsk_,sk-,service_role, and the first few characters of each of your keys. - If anything turns up that the table above says should never be public, treat it as leaked.
If a key has leaked
Deleting it from the code is not enough: it's already been sent to browsers, and it stays in your Git history.
- Revoke or rotate the key now in that service's dashboard (Stripe, OpenAI, Supabase and so on) and create a new one.
- Put the new key in your hosting provider's settings, without a public prefix, and move the code that uses it to the server.
- Redeploy, then check your billing and usage pages for anything you don't recognise.
Quick checklist
.envis in.gitignore, and a.env.exampleis committed- No secret key uses
NEXT_PUBLIC_,VITE_,REACT_APP_orEXPO_PUBLIC_ - Secret calls (AI, payments, admin database access) happen on the server
- Supabase Row Level Security is on if you use the anon key in the browser
- Every variable is set in your hosting dashboard, and you redeployed after setting it