Never put a secret key in React code or in a REACT_APP_ variable: anything a browser app uses is shipped to every visitor and readable in the bundle. Keep secret keys on a server, and have your React app call your own backend, which attaches the key. Only publishable identifiers meant for browsers belong in client-side config.
Read the key from the environment
// React runs in the browser: do NOT read secrets here.
// Call your own backend, which holds the key and calls the third party.
export async function createCharge(amount) {
const res = await fetch("/api/charges", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ amount }),
});
if (!res.ok) throw new Error("Request failed");
return res.json();
}Local development
In a Create React App project, variables starting with REACT_APP_ are embedded into the JavaScript bundle at build time, and the docs warn not to store secrets in them. Other toolchains (Vite, Next.js, Webpack) have their own public prefixes; see their guides. For local work, keep real secret keys in a server-side .env for your backend (for example an Express or serverless function) and put only publishable keys in the front end.
Production
Deploy the secret on the server or serverless function, using its platform secret store, and expose a narrow API your React app calls. Authenticate users to that API with sessions or short-lived tokens, and rate-limit it so a leaked endpoint cannot be abused. If a third party offers a browser-safe, restricted key (for example a publishable key limited by origin), use that in the client instead.
Mistakes to avoid
- Putting a secret in
REACT_APP_*(orVITE_*,NEXT_PUBLIC_*): the build inlines it as a string in the public bundle, so anyone can read it from the browser's network or sources tab. - Assuming minification or obfuscation hides a key; it does not, and source maps can make it more readable.
- Calling a third-party API directly from the browser with a secret key because it works in development; move the call behind your own backend.
- Treating a
.envfile as private because it is not served: the build step copies itsREACT_APP_values into the output.
When an AI agent works on your React 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
Can I store an API key in a React app?
Only a key designed for public use, such as a publishable or origin-restricted key. A secret key must live on a server, because everything in a React bundle is visible to users.
Are REACT_APP environment variables secret?
No. Create React App embeds them in the built JavaScript at build time, so they are readable by anyone who loads your site.
Should I keep API keys in a .env file for React?
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.