SyncNode logo
Frontend Key Guide

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.

Updated November 10, 2026 · 6 min read · $5 free credit, no subscription

The short answer

  1. A .env file doesn't hide a key from the browser. VITE_, NEXT_PUBLIC_, REACT_APP_ and EXPO_PUBLIC_ values ship to every visitor.
  2. Check by searching your built files for the start of your key.
  3. Move the AI call into a server route, and have the browser call that.
  4. Protect the route with sign-in and a per-user limit.
  5. 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.

FrameworkPublic prefixWhere your API key belongs
Vite (including React with Vite)VITE_A serverless function or your own server
Next.jsNEXT_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 NativeEXPO_PUBLIC_A backend your app calls

How to fix it

  1. 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.

  2. 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.

  3. 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.

  4. Call your route from the browser

    Your React code sends the user's input to /api/ask and gets the answer back. The key never leaves the server.

  5. 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.

Prompt
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.

Next.js: server route and browser call
// 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.
Pricing: $0.02 per AI call and $0.005 per moderation check, with no subscription and $5 of free credit to start. See full pricing.

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.

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
SyncNode dashboard showing tasks, costs and results