# Are VITE_ environment variables safe for API keys?

Canonical: https://caelo.sh/guides/vite-env-variables-exposed
Published: 2026-11-27 · Updated: 2026-11-27 · Arthi Arumugam, Caelo by Irth Labs

**Short answer:** No. Vite exposes every `VITE_`-prefixed variable to your client code through `import.meta.env` and writes the value into the built JavaScript. Vite's own docs say `VITE_` variables should not contain sensitive information such as API keys. A Vite app runs entirely in the browser, so calls that need a secret key belong in a backend or an edge function.

## What does Vite expose?

Vite's docs say: "Variables prefixed with `VITE_` will be exposed in client-side source code after Vite bundling", and "`VITE_*` variables should _not_ contain sensitive information such as API keys." Unprefixed variables in your `.env` files are not exposed to client code.

The file name does not help. `.env.local` is ignored by git if you follow Vite's advice to add `*.local` to `.gitignore`, but a `VITE_` value inside it is still written into the bundle you deploy. Git-ignored and secret are different things.

## The vulnerable pattern and the fix

Vulnerable: a server-only key read in browser code

```ts
// src/lib/email.ts
const res = await fetch("https://api.resend.com/emails", {
  method: "POST",
  headers: { Authorization: "Bearer " + import.meta.env.VITE_RESEND_API_KEY },
  body: JSON.stringify(email),
});
```

Fixed: a Supabase Edge Function holds the key

```ts
// supabase/functions/send-email/index.ts
import { withSupabase } from "npm:@supabase/server@1";

export default {
  fetch: withSupabase({ auth: "user" }, async (req, ctx) => {
    const { subject, html } = await req.json();
    const res = await fetch("https://api.resend.com/emails", {
      method: "POST",
      headers: {
        Authorization: "Bearer " + Deno.env.get("RESEND_API_KEY"),
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ from: "app@example.com", to: ctx.userClaims?.email, subject, html }),
    });
    return Response.json({ ok: res.ok });
  }),
};
```

The browser calls the function with the signed-in user's token

```ts
const { data, error } = await supabase.functions.invoke("send-email", {
  body: { subject: "Your report", html: "<p>Ready.</p>" },
});
```

Set the key with `supabase secrets set RESEND_API_KEY=...`. It never appears in your repository or your bundle. Any backend works the same way: a Vercel function, a Cloudflare Worker or an Express server.

## The loadEnv trap: a key with no VITE_ prefix can still ship

Vite lets a config file load every variable with `loadEnv(mode, process.cwd(), "")`, where the empty third argument means "regardless of the `VITE_` prefix". That is fine for configuring the dev server. It becomes a leak when the config then copies a value into client code with `define`:

Vulnerable: vite.config.ts inlines an unprefixed secret

```ts
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  return {
    define: {
      "process.env.GEMINI_API_KEY": JSON.stringify(env.GEMINI_API_KEY), // now in the bundle
    },
  };
});
```

Every `define` entry is replaced in your source at build time, so the key ends up in the JavaScript even though its name has no prefix. Remove the `define` line and move the call to a server. Separately, Vite refuses `envPrefix: ""` in the config: its docs say an empty prefix would expose all your variables and that Vite throws an error when it detects one.

## Fix it, step by step

1. List the public variables: `grep -rhoE "VITE_[A-Z0-9_]+" src | sort -u`, and read `vite.config` for `define` and `loadEnv`.
2. Keep only publishable values behind `VITE_`: a Supabase URL and anon or publishable key, a Stripe publishable key, Firebase web config, analytics IDs.
3. Move every other call into an edge function or backend that reads the key from its own environment.
4. Rotate every key that was ever behind `VITE_` or in a `define`. Old deployments and anyone who loaded them still have it.
5. Build again and search the output: `npm run build && grep -rl "sk-\|re_\|AIza" dist` should return nothing that is a secret.

## Check it automatically

Caelo flags a `VITE_` name that looks like a secret or belongs to a server-only provider (`browser.public-secret-name`), and any provider key it recognises written directly in a file, such as an OpenAI or Google key. It does not evaluate your `vite.config`, so it will not catch the `define` pattern above when the value comes from your environment at build time. Read that file yourself.

Part of the real report from the demo app in the Caelo README

```text
- Server-only key exposed to the browser [needs review]
    src/lib/ai.ts:1  (browser.public-secret-name)
    value: VITE_OPENAI_API_KEY
    why: VITE_OPENAI_API_KEY has a browser-public prefix, so its value is bundled into the JavaScript every visitor downloads. This provider's keys are server-only; there is no browser-safe version.
```

- [Install Caelo: npx caelo . or the MCP server](/)
- [How Caelo compares with gitleaks, TruffleHog, Semgrep and others](/compare)

## Check your own app

1. Run the free local check: `npx caelo .` Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and `.env` files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Follow the fix in the report and rerun Caelo. For an untracked, unignored file containing secrets, preview the US$19 quick fix. https://caelo.sh/fix
3. For an engineer's assessment, book the US$490 App Health Check. Irth Labs delivers a report and fixed repair quote, with credit toward the repair. https://www.irthlabs.com/start?service=app-health-check

## Questions

### Is VITE_SUPABASE_ANON_KEY safe in my bundle?

Yes. The anon key and the publishable key are meant to be public, and row level security decides what they can reach. Check your policies with our RLS guide.

### Does putting the key in .env.local keep it private?

It keeps the file out of git. If the variable starts with `VITE_`, the value is still written into the built JavaScript, so the key is public as soon as you deploy.

### Can I obfuscate or encode the key in the frontend?

No. The browser needs the real value to send it, so anyone can read it from the network tab. The only fix is to keep the key on a server.

## Sources

- [Vite docs: Env variables and modes.](https://vite.dev/guide/env-and-mode)
- [Vite docs: envPrefix option.](https://vite.dev/config/shared-options#envprefix)
- [Vite docs: Using environment variables in config (loadEnv).](https://vite.dev/config/#using-environment-variables-in-config)
- [Supabase docs: Securing Edge Functions (withSupabase).](https://supabase.com/docs/guides/functions/auth)
- [Supabase docs: Edge Function secrets.](https://supabase.com/docs/guides/functions/secrets)
- [Caelo source: every rule in lib/rules.js.](https://github.com/arthi-arumugam-git/caelo/blob/master/lib/rules.js)

## Related guides

- [Is my Lovable app secure? A 15-minute self-check](https://caelo.sh/guides/is-my-lovable-app-secure)
- [Is my Bolt.new app secure? A self-check before you launch](https://caelo.sh/guides/is-my-bolt-app-secure)
- [I leaked an API key or the Supabase service_role key. What do I do now?](https://caelo.sh/guides/leaked-api-key-what-to-do)
- [Are NEXT_PUBLIC_ environment variables secret? What leaks, and how to fix it](https://caelo.sh/guides/next-public-env-variables-secrets)
- [OpenAI API key exposed in client-side code: how to fix it](https://caelo.sh/guides/openai-api-key-exposed-frontend)
