Guide

How to store API keys in Vite safely

Vite exposes only environment variables that start with VITE_ to client code through import.meta.env, and it inlines them into the built files. That means a VITE_ variable is public, so never put a secret there. Keep secret keys on a backend that your Vite app calls.

Read the key from the environment

// Client code (browser): only public values, via import.meta.env
const apiBase: string = import.meta.env.VITE_API_BASE_URL;

// Secrets belong on the server. Call your own API instead:
const res = await fetch(`${apiBase}/charges`, { method: "POST" });

Local development

Vite loads .env, .env.local and mode-specific files such as .env.production, and only variables with the envPrefix (default VITE_) reach import.meta.env. The docs advise adding .env*.local to .gitignore. For server-side tooling that runs in Node, such as vite.config.ts, you can read other variables with loadEnv, but do not forward them into client code via define.

Production

In production a Vite app is static files, so it cannot hold a secret. Put anything sensitive in a backend, edge function or serverless function that your platform configures with secret environment variables, and have the front end call it. If you use Vite SSR, keep secret reads inside server-only code paths.

Mistakes to avoid

  • Setting envPrefix: '' or adding a broad prefix, which exposes every variable to client code; Vite's docs warn against this.
  • Putting a secret in a VITE_ variable or in define in vite.config.ts: the value is written into the public JavaScript bundle at build time.
  • Believing .env.local is private once built: its VITE_ values are copied into the output files.
  • Deploying source maps publicly while secrets are present in the bundle, which makes them easier to find.

When an AI agent works on your Vite project

Coding agents run inside your project folder, so they can read the same .env file your app reads. BotPassword injects keys at run time instead: the agent calls the run_with intent through the MCP server and the process gets the variables it needs while the agent sees only redacted output. Planned: bpw run, a CLI form of the same.

Frequently asked questions

Are VITE_ environment variables secure?

No. Vite inlines VITE_ variables into the client bundle, so anyone can read them. Use them only for public configuration such as an API base URL.

Where should I keep a secret key in a Vite project?

On a server. Create a backend or serverless function with the key stored in the platform's secret settings, and have your Vite app call that endpoint.

Should I keep API keys in a .env file for Vite?

A .env file that is git-ignored is fine for local development, but every process and every AI coding agent running in that folder can read it. Load secrets into the environment at run time from a password manager or secrets manager instead, so no plaintext file sits in the project.

Other languages and frameworks

Give agents access. Keep the keys.

BotPassword is in early access. Request an invite and we'll email you when your spot opens.