9 min read · Updated 2026-11-29
Lovable + OpenAI: how to prevent a surprise bill
If a Lovable app talks to OpenAI, here is how the key ends up in the browser and the prompt to paste so the call moves server-side.
Lovable builds a real website. The browser downloads the JavaScript. Anything in that JavaScript is public, including a key you pasted into the chat, dropped in a file, or stored under a name that starts with VITE_ or NEXT_PUBLIC_.
The usual surprise looks like this. The app works. You close the laptop. Someone views source, copies the key, and scripts a loop against api.openai.com. The bill is yours, because the key is yours. Lovable did what you asked. The browser did what browsers do.
Check the live site, not the editor
- Open the deployed URL, not the Lovable preview pane.
- View the page source and search for sk-proj-, sk-ant-, or AIza.
- Open the network tab, send one chat message, and see who you called. If the request goes to api.openai.com from the browser, the key is in the page or the user is supplying one.
- Search the project for NEXT_PUBLIC_ and VITE_. Those prefixes mean “ship this to every visitor.”
Paste this into Lovable
Paste this into Lovable, Bolt, or Cursor
The OpenAI key must not be in client code. Move the model call to a server route or a Supabase Edge Function. Requirements: - Delete the key from every file the browser downloads, and from any variable starting with VITE_ or NEXT_PUBLIC_. - Store the key as a server secret. Do not print it. - The browser calls only /api/chat (or the edge function). It never calls api.openai.com. - Require a signed-in user. Return 401 if there is no session. - Cap each user and each IP at a small daily number of calls, stored in the database, not in a memory variable. - When the cap is hit, return a plain “daily limit reached” message. Do not invent a new key in code. I will paste the secret into the host's secret settings myself.
Set the provider cap the same day
Even after the code is fixed, set a hard monthly spend limit on the OpenAI organization and on the project. Code fixes take a deploy. The limit is on already. Pick a number you can lose, turn on Enforce a hard limit, and add an alert at half of it. Details are in the spending-limit guide on this site.
How you know it worked
- View source on the deployed site and search again. The key string is gone.
- The network tab shows a call to your own domain, not api.openai.com.
- A logged-out request to that route returns 401.
- BrakeKey's free scan on the public URL no longer reports a key.