8 min read · Updated 2026-11-29
Hide AI keys in Bolt, Cursor, and Replit
Why NEXT_PUBLIC_ and VITE_ keys are public, and the server-side pattern to ask Bolt, Cursor, or Replit for instead.
Bolt, Cursor, and Replit will put a key exactly where you tell them to. If you say “add my OpenAI key so the chat component can call the API,” the tool often drops it in the component, because that is the file you had open. The component ships to the browser. The key goes with it.
The prefix rule
If the variable name starts with NEXT_PUBLIC_, VITE_, or REACT_APP_, the build is designed to include the value in public JavaScript. That is useful for a map tile URL. It is the wrong place for a secret. A variable without that prefix, read only in a server file, stays on the server — unless you import that file from a client component, which pulls it back into the bundle.
- Bolt: put the key in the project's server secrets, and tell Bolt the chat component may only call your own route.
- Replit: use the Secrets pane for the server process. A secret referenced from a static HTML file or a client script is still public.
- Cursor: the prompt is the whole control. Ask for a server route by name, and forbid the public prefixes.
Paste this
Paste this into Lovable, Bolt, or Cursor
Move the AI call off the client. Create a server route at /api/chat. Read the provider key from a server environment variable. The name must not start with NEXT_PUBLIC_, VITE_, or REACT_APP_. The client component fetches /api/chat only. It must not contain the key, and it must not call api.openai.com, api.anthropic.com, openrouter.ai, or generativelanguage.googleapis.com. Require a signed-in user. Add a per-user and per-IP daily limit. Show me the file that reads the key so I can confirm it is a server file.
Check the built site
The editor can look clean while the last deploy is still bad. Open the public URL, view source, and search for the key prefix. Then run BrakeKey against that same URL. Fix whatever it lists, redeploy, and scan once more.
Set a hard spend limit at the provider before you go to bed. The code change protects the next deploy. The limit protects tonight.