VITE_ environment variables are public: what to move to the server

Short answer: any variable whose name starts with VITE_ is copied into your app's JavaScript when the app is built, so anyone who opens your site can read it. Keep only values that are meant to be public there, such as your Supabase URL and publishable key. Move every secret key (payment, email, AI provider, database admin keys) into server-side code, such as a Supabase Edge Function, and store it in a secrets manager. Then replace the old key at the company that issued it, because a key that was ever in a public build should be treated as exposed.

Why VITE_ values are public

Vite, the build tool behind Lovable apps and many React projects, says it plainly: variables prefixed with VITE_ "will be exposed in client-side source code after Vite bundling." Its docs add that VITE_* variables "should not contain sensitive information such as API keys," because their values are bundled into your source code at build time.

So the .env file is not the problem by itself. The prefix is. A value named DB_PASSWORD stays on the build machine. The same value named VITE_DB_PASSWORD ends up in the files your visitors download.

Renaming the prefix does not help. Vite lets you change it with the envPrefix option, but anything matching the new prefix is exposed in the same way. Vite also refuses an empty prefix, because that would expose every variable.

Which values can stay, and which must move

Usually fine as VITE_ (designed to be public)Must move to the server
Supabase project URLSupabase secret key (sb_secret_...) or legacy service_role key
Supabase publishable key (sb_publishable_...) or legacy anon key, only if RLS is set up correctlyStripe secret key (sk_...) or restricted key (rk_...)
Stripe publishable key (pk_...)Stripe webhook signing secret
Any other value only if that provider's own docs say it is safe to expose in the browserAI provider keys (OpenAI, Anthropic and others)
Email provider keys (Resend, SendGrid and others)
Database passwords and connection strings
Any key whose provider docs say "keep it secret"

The sources for the rows above: Supabase says the publishable key is safe to expose while secret keys bypass RLS and must never be in a browser. Stripe says publishable keys are safe to expose and that secret and restricted keys are not. Lovable lists STRIPE_SECRET_KEY, RESEND_API_KEY and OPENAI_API_KEY as values that belong in Secrets, and VITE_SUPABASE_URL and VITE_SUPABASE_PUBLISHABLE_KEY as values that are safe to be public.

A public Supabase key is only safe when your database rules are right. If you have not checked them, read our guide on checking Row Level Security before launch.

Step 1: Find every VITE_ value

In your project folder, list the names of your VITE_ variables. This command prints only the names, never the values, so no key ends up on your screen, in your terminal history, in a screen recording or in the context of an AI assistant that runs your terminal:

grep -rhoE "VITE_[A-Za-z0-9_]+" .env* src | sort -u

For each one, ask: "Would I be fine if a stranger had this value?" If the answer is no, it has to move.

You can also check what actually shipped. Build the app and search the output folder (Vite uses dist by default) for the start of a known secret format, without pasting the full secret anywhere:

npm run build
grep -rlE "sb_secret_|sk_live_|rk_live_|sk_test_|rk_test_" dist

The -l option prints only file names, not the secrets. Any file name in the result means a secret is inside the code your visitors download. The prefixes come from Supabase (sb_secret_) and Stripe (sk_ and rk_ keys, live and test). For other providers, add a prefix only if their own docs publish it.

Older Supabase projects use long keys that start with eyJ for both the public anon key and the secret service_role key, so a prefix search cannot tell them apart. If you find an eyJ value in your build, compare it with the keys listed under Settings → API Keys in your Supabase Dashboard. Do not paste it into an online JWT decoder. If it is the service_role key, treat it as leaked and follow our guide on the first hour after a service_role key leak.

Step 2: Move the call to the server

The secret key should live where only your server code can read it. The browser then calls your server code, and the server code calls the outside service.

On Lovable: Lovable's docs recommend describing what you want instead of pasting keys, for example "Connect Stripe so clients can pay invoices online. Ask me for any API keys you need." Lovable stores the key in Secrets, which are injected into Edge Functions and never reach the browser. Lovable rejects VITE_ names in Secrets, because those are build-time browser values. Keep that .env file in place: Lovable's docs say not to add it to .gitignore, because the build needs the public VITE_ values. That file is only for public values. It is not the same as the server secrets file described below.

On Supabase directly: keep server secrets in a separate file, supabase/functions/.env, and add that file to .gitignore. Supabase's docs warn that a committed .env file exposes every secret in it to anyone who can read the repository. Write the key into that file with your editor, then push the file's secrets to your project:

supabase secrets set --env-file supabase/functions/.env

This way the key never appears on the command line, so it does not land in your shell history. You can also set secrets in the Dashboard. Then read the secret in your function:

const stripeKey = Deno.env.get('STRIPE_SECRET_KEY')

Supabase's docs show both commands and note that secret names cannot start with SUPABASE_.

Step 3: Make the server function check who is calling

Moving a key to the server is only half the fix. Lovable's docs say "A function running on the server is still callable by a client," so every function must check who is calling and whether they are allowed to do what they ask.

Supabase documents a wrapper for this. With auth: 'user', the function only runs for a signed-in user and gives you a database client that follows that user's RLS rules:

import { withSupabase } from 'npm:@supabase/server@1'

export default {
  fetch: withSupabase({ auth: 'user' }, async (_req, ctx) => {
    // ctx.supabase follows the signed-in user's RLS policies
    // ctx.supabaseAdmin bypasses RLS: use it only when you must
    return Response.json({ email: ctx.userClaims?.email })
  }),
}

Supabase warns that a handler using ctx.supabaseAdmin on a shared table without filtering by the caller's ID returns every user's rows.

Step 4: Replace the old key at the issuer

Deleting the variable and rebuilding does not undo the exposure. Earlier builds, browser caches and your Git history may still hold the value. Liquid's own pre-production checklist says it this way: if a real credential was exposed, revoke or rotate it at its issuer, because deleting the visible string alone is not enough.

  • Supabase: create a new secret key, switch your server code to it, confirm everything works, then retire the old one. See our guide on what to do in the first hour after a service_role key leak.
  • Stripe: rotate the key on the API keys page. Stripe says both keys can work for up to 7 days during rotation so you can switch without downtime, or you can expire the old key right away.
  • Other providers: follow their key rotation docs.

FAQ

Is it safe to have my Supabase anon or publishable key in the browser?

Yes, that is what it is for. It is only safe if Row Level Security is on and the policies only allow what you intend.

My key was only in a VITE_ variable for a day. Do I still need to rotate it?

If it was in a build that was published, treat it as exposed. You cannot know who downloaded the files.

Can I hide the key by encoding it or splitting it into parts?

No. The browser has to rebuild the value to use it, so anyone can do the same. Vite's guidance is to use a backend or serverless function for secrets.

Where should non-secret config go in a Lovable app?

Lovable says VITE_ values that are safe to be public belong in the .env file, and private values belong in Secrets.

Try the Liquid sandbox

Once your secret calls live in server code, the sandbox can give that code a first look. It also flags some credential-like patterns in that file. A match is a lead to review, not proof of a leak, and no match does not mean your code has no secrets.

You can paste JavaScript or TypeScript code you own, including JSX and TSX, up to 12,000 bytes and with no real keys or personal data, into the Liquid code review sandbox. It reads the code in your browser without running it and without sending it to a server, with no signup, no upload and no saved history. It checks 8 fixed rules: eval or other dynamic code, shell commands, disabled TLS certificate checks (rejectUnauthorized: false), MD5 or SHA-1 (for information only), and request data reaching a network call, a SQL string, HTML output, or the choice of which database record to load. It reuses the product's single-file AST rules and some credential-like patterns. A match is a lead to review, not proof of a leak, and no match does not mean your code has no secrets. It does not check Supabase RLS, buckets or settings, check dependencies, read a repository or a live site, or confirm a fix. A result is a starting point for review and never approves code for production.

Sources

Get my free grade