Short answer
An environment variable is a named value, such as an API key or URL, that a program reads from its environment instead of from its source code. In a Vite or Lovable app, every variable that starts with VITE_ is copied into the JavaScript that visitors download, so it is public. Secrets belong in server-side secrets, such as Supabase Edge Function secrets, and never in a VITE_ variable.
Ask an AI about this page:ChatGPTClaude
On this page
An environment variable is a named value that a program reads from the environment it runs in, instead of having it written into the source code. URLs, feature flags and API keys are typical examples. The idea is that the same code runs in development and production with different values, and that secrets stay out of the repository.
Environment variables in an AI-built app
Lovable, Bolt and most React apps use Vite, and Vite has one rule that matters more than any other here: variables that start with VITE_ are exposed to your client code through import.meta.env, and their values “are bundled into your source code at build time”, as the Vite docs put it. Vite’s docs say plainly that VITE_* variables should not contain sensitive information such as API keys.
So there are two very different places a value can live:
| Where | Example | Who can read it |
|---|---|---|
VITE_ variable in the frontend | VITE_SUPABASE_PUBLISHABLE_KEY | Every visitor, in the downloaded JavaScript |
| Server-side secret | Edge Function secret OPENAI_API_KEY, read with Deno.env.get | Only your server code |
The classic mistake: the AI tool needs an OpenAI or Stripe secret key “in the app”, adds VITE_OPENAI_API_KEY to .env, and calls the API straight from the browser. It works, and the key is now public. Supabase’s docs also warn that the legacy JWT secret leaks easily through VITE_ or NEXT_PUBLIC_ variables.
How to check your app
- List every
VITE_variable in.env,.env.*and your hosting settings. For each one ask: would I be fine with this value on a billboard? Supabase URL and publishable key: yes. Anything withsecret,service,sk_orsb_secret_: no. - Build the app and search the output folder (
dist/) for the start of each secret you own, for examplesk_live,sk_testorsb_secret_. - Check that
.envand*.localfiles are in.gitignore, and search your git history for keys that were committed earlier. - In edge functions, check that values read with
Deno.env.getare never returned in a response or written to logs.
How to fix or prevent it
Move the secret to the server: store it as an Edge Function secret (supabase secrets set NAME=value), call the third-party API from an edge function, and let the browser call that function instead. Then treat the old key as leaked and rotate it, because every visitor could already copy it. The step-by-step fix is on Secret key in a VITE_ variable, and which keys may live in the frontend at all is explained in API keys in AI-built apps.
Frequently asked questions
Is a value in .env secret because .env is not committed?
Only if your code never sends it to the browser. In a Vite app, any VITE_ value is written into the built JavaScript, whether .env is in git or not.
Which values may safely be VITE_ variables?
Values that are public by design: your Supabase URL, the Supabase publishable or anon key, a Stripe publishable key, an analytics ID. Nothing that starts with sk_, sb_secret_ or is called a secret or service key.
Are environment variables on the server a safe place for secrets?
They are the common and accepted option when no secrets manager is available. OWASP notes they can leak through logs, crash dumps and child processes, so never print them or return them in responses.
Sources
- 1.Env Variables and Modes — Vite
- 2.Environment variables (secrets) — Supabase Docs
- 3.Secrets Management Cheat Sheet — OWASP Cheat Sheet Series
- 4.Understanding API keys — Supabase Docs