How to hide your API key in React, Vite and Next.js
If your React app calls OpenAI, Replicate or any AI API straight from the browser, your key is public, even if it lives in a .env file. Here's how to check in a minute, and how to fix it properly, whether an AI builder wrote your app or you did.
The short answer
- A
.envfile doesn't hide a key from the browser.VITE_,NEXT_PUBLIC_,REACT_APP_andEXPO_PUBLIC_values ship to every visitor. - Check by searching your built files for the start of your key.
- Move the AI call into a server route, and have the browser call that.
- Protect the route with sign-in and a per-user limit.
- If the key was ever public, replace it.
Why .env doesn't hide your key
Build tools copy public variables into your code
Vite's docs say VITE_ variables are exposed in client-side code and shouldn't contain API keys. Next.js inlines NEXT_PUBLIC_ values into the JavaScript sent to the browser.
Removing the prefix just breaks the call
Without the public prefix, browser code can't read the variable at all, so the request fails. That's usually the moment someone adds the prefix back, and the key goes public.
Mobile apps aren't different
Expo's docs say EXPO_PUBLIC_ values are visible in plain text in your compiled app, so the same rule applies on phones.
Which variables end up in the browser
Each framework has a prefix that means “send this to the browser.” Anything with that prefix is public, no matter which file it's in.
| Framework | Public prefix | Where your API key belongs |
|---|---|---|
| Vite (including React with Vite) | VITE_ | A serverless function or your own server |
| Next.js | NEXT_PUBLIC_ | A Route Handler or other server code, in a variable without the prefix |
| Create React App (deprecated) | REACT_APP_ | A separate backend |
| Expo and React Native | EXPO_PUBLIC_ | A backend your app calls |
How to fix it
-
Check whether your key is exposed
Build your app and search the output for the first few characters of your key, or open your live site, press F12 and search the Sources tab. If it shows up, it's public.
-
Replace the key if it was ever public
If that code was deployed, assume someone copied the key. Create a new one at the provider and delete the old one. The leaked key checklist covers the rest.
-
Add a server route
In Next.js that's a Route Handler such as
app/api/ask/route.ts. With Vite, it's a serverless function on your host or a small server. It reads the key from a variable without a public prefix. -
Call your route from the browser
Your React code sends the user's input to
/api/askand gets the answer back. The key never leaves the server. -
Protect the route
Require sign-in and limit how often each user can call it. Otherwise anyone can use your route as a free AI service, and you pay.
Why SyncNode is great behind your server route
Your route needs one secret either way. With SyncNode, that one secret covers every provider, and moderation and call logs come with it.
One server variable for every model
Use OpenRouter, Replicate, fal and more through a single SYNCNODE_API_KEY. Adding a provider doesn't add another secret to your hosting settings.
Switch models without new secrets
Trying a different model is a change to one string in your route, with nothing new to configure on your host.
Moderation in the same route
Check the user's text or image with one extra request before it reaches the model. No separate vendor and no extra provider key.
A leak stays small
If your SyncNode key leaks, it can't reveal your provider keys. Rotate it and the old one stops working within 30 seconds.
Slow jobs are handled for you
Video and other long jobs run in the background. Your route starts the job, then checks its status, and SyncNode keeps track of it.
Files that last
Connect a storage host and generated images and video are copied there, so your app isn't relying on a provider's temporary link.
Ask your AI assistant to fix it
Paste this into Lovable, Bolt, Cursor or whichever assistant built your app. First add your OpenRouter key on the SyncNode API Keys page and create a sn_live_ key.
Move every AI API call out of the browser.
Create a server route POST /api/ask. It reads the key from a server-only environment variable named SYNCNODE_API_KEY, with no VITE_ or NEXT_PUBLIC_ prefix. It must require a signed-in user and limit each user to 20 requests per hour.
The route POSTs to https://run.syncnode.ai/chat-completion with { apiKey, model: "openai/gpt-4o-mini", messages }.
Update the frontend to call /api/ask instead of the AI provider, and remove every API key from frontend code and public environment variables. API reference: https://syncnode.ai/llms.txt
In Next.js, the two halves look like this: the route runs on the server, and the component only talks to your route.
// app/api/ask/route.ts (runs on the server)
const KEY = process.env.SYNCNODE_API_KEY; // no NEXT_PUBLIC_ prefix
export async function POST(req: Request) {
// Check the signed-in user and their hourly limit here first.
const { text } = await req.json();
const res = await fetch("https://run.syncnode.ai/chat-completion", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
apiKey: KEY,
model: "openai/gpt-4o-mini",
messages: [{ role: "user", content: text }]
})
});
return Response.json(await res.json());
}
// In your React component (runs in the browser, no key here)
const res = await fetch("/api/ask", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text })
});
const answer = await res.json();
What SyncNode doesn't fix
No tool removes all the risk, so here's the honest list.
- You still need a server route. SyncNode can't make it safe to call an AI API straight from the browser. Put any key in frontend code and it's public again.
- Your route still needs protecting. Sign-in and per-user limits are up to your app. SyncNode doesn't add them for you.
- Trust moves to SyncNode. Your provider keys live with us, so keep a spending limit at each provider too.
- There's a small fee. It's $0.02 per AI call and $0.005 per moderation check, on top of your provider's charges.
Common questions
Only if server code reads it. In Vite, VITE_ values are built into the client. In Next.js, NEXT_PUBLIC_ values are inlined into the browser bundle. Unprefixed variables stay on the server.
No. The browser needs the real key to make the call, so anyone can read it from the network tab. The only fix is keeping the key on a server.
Yes, as long as the variable has no NEXT_PUBLIC_ prefix and only server code reads it. The Next.js docs say non-prefixed variables are only available in the Node.js environment.
Ask it to move the call into a server function with the prompt above, then replace the key, since the old one was public.
More guides
How to keep your API keys safe in Lovable
Read the guideHow to keep your API keys safe in Bolt.new
Read the guideHow to keep your API keys safe in Replit
Read the guideHow to keep your API keys safe in Cursor
Read the guideLeaked an API key? Do this right now
Read the guideHow to protect yourself as a vibe coder
Read the full checklistThe framework details come from each project's official docs, checked on November 10, 2026:
- Vite: Env variables and modes
- Next.js: Environment variables
- Expo: Environment variables
- Create React App: Custom environment variables
Product names are trademarks of their owners. SyncNode isn't affiliated with or endorsed by them. This guide is general information, not legal or security advice for your specific situation.
Ship fast. Stay protected.
Join the builders behind 9M+ AI runs on SyncNode. Lock away your keys and moderate your users in one afternoon, with $5 free to start.
Start free