What are environment variables?
The same code runs in preview and in production with different settings. Environment variables are how, and the prefix on their name decides whether every visitor can read them.

Key takeaways
- Environment variables keep configuration and keys out of the code, so one codebase can run with different settings in each environment.
- Variables prefixed VITE_, NEXT_PUBLIC_ or EXPO_PUBLIC_ are baked into the frontend and are public by design.
- Real secrets belong in a server-side secrets store; a committed .env file stays in git history even after you delete it.
Software reads environment variables, named settings such as `API_URL` or `STRIPE_SECRET_KEY`, from the place it runs rather than from its own code. They let the same app talk to a test payment account in preview and the live one in production, and they keep keys out of the files you share, push to GitHub or paste into a chat.
An everyday example: one recipe, different kitchens
A cafe chain uses one recipe card for its soup, but the card says "use the house stock" rather than naming a supplier. Each branch keeps its own stock. The recipe never changes; what it picks up depends on the kitchen. Code works the same way: it says `process.env.SUPPORT_EMAIL` or `import.meta.env.VITE_API_URL`, and each environment fills in its own value.
Most apps have at least two kitchens: a development or preview environment where you try things, and production, where real users and real money live. A third, staging, is a copy of production for a final check. The variables are what tell them apart: a test payment key here, the live one there; a sandbox email sender here, the real domain there. Mixing them up is how test orders end up charging real cards, or real customers receive test emails.
A typical `.env` file is just lines of names and values:
- `VITE_SUPABASE_URL=https://abcd.supabase.co` (public, used by the frontend)
- `VITE_SUPABASE_ANON_KEY=eyJ...` (public by design, limited by row-level security)
- `RESEND_API_KEY=re_...` (secret, server only)
Public variables versus secret ones
This is where most leaks happen. Frontend build tools only expose variables with a special prefix: `VITE_` for Vite, `NEXT_PUBLIC_` for Next.js, `EXPO_PUBLIC_` for Expo. They copy the value straight into the JavaScript every visitor downloads. That is fine for a public URL or a publishable key. It is a leak for anything that bills you or bypasses permissions. Rename `OPENAI_KEY` to `VITE_OPENAI_KEY` "to make it work" and you have just published it.
Secrets go in the server environment: an edge function or API route reads them at run time, and the browser only ever sees the result.
Why it matters when building with Startbase
Startbase draws that line for you. Backend secrets live in Backend, Secrets: encrypted at rest, read by edge functions as environment variables, never exposed to client-side code, and write-only, so nobody can read a saved value back. Names starting with `VITE_`, `EXPO_PUBLIC_` or `NEXT_PUBLIC_` are rejected there, because those are client-side variables that belong in the app's `.env` in the workspace. Secrets starting with `SUPABASE_` are provisioned and rotated by Startbase Cloud (secrets docs). Before every publish, a security check also looks for leaked keys and committed `.env` files.
I want to send order confirmation emails with my email provider. Do not put the API key in the frontend. Create an edge function that sends the email, read the key from a secret named EMAIL_API_KEY, tell me where to add it in Backend Secrets, and call the function after checkout succeeds.
More on keys in API keys and secrets. If you deploy outside Startbase, the hosting options page notes that `VITE_` variables are embedded at build time, so set them on the new host before building.
Common confusions
- Changing a VITE_ variable needs a rebuild. Build-time values are frozen into the published files; republish after you change one.
- Deleting a committed .env does not un-leak it. It stays in git history. Rotate the key at the provider.
- Environment variables are not a database. They are for configuration that rarely changes, not per-user settings.
Frequently asked questions
Should I commit my .env file to GitHub?
No. Commit a `.env.example` with names and placeholder values instead, so others know which variables to set, and keep real values out of the repository.
Is the Supabase anon key safe in a VITE_ variable?
Yes, it is designed for the frontend and only grants what row-level security allows. The service role key is the opposite and must never be in a client-side variable.

