Toupas

Blog · 17 November 2026

Is it safe to put an API key in your frontend code?

You asked an AI to add a feature that calls OpenAI, Claude, Gemini or a payment service. It works. Then you notice the key is sitting in a JavaScript file, or in a .env value that ends up in the page.

No, it isn't safe. Anything the browser downloads can be read by anyone who opens your app. Here's why, where the key should go instead, and what to tell your agent.

A key in the page can be read by anyone who opens the app. A key kept on the server is added to the request there, so the browser never sees it.

Why the browser can't keep a secret

Your frontend is a set of files sent to every visitor: HTML, CSS and JavaScript. To run them, the browser has to have them, in full. So anyone can:

Minifying the code, splitting the key into pieces or encoding it doesn't help. The page has to put the key back together to use it, so anyone can do the same.

This applies to build tools too. In Vite, anything named VITE_… is copied into the page. In Next.js, NEXT_PUBLIC_… is. Those prefixes exist precisely to mark values as safe to make public. Don't put a secret key in one.

What happens when a key leaks

It depends on the key, and none of it is good:

Some keys are meant to be public: a payment service's *publishable* key, a maps key restricted to your domain, or a sign-in service's client ID. The service's docs say so plainly. If they don't say it's safe for the browser, assume it isn't.

Where the key goes instead

The key belongs on a server, and the browser asks your server to make the call:

  1. The page sends "summarise this note" to your app's own server.
  2. The server adds the key and calls the AI service.
  3. It sends back only the answer.

The browser never sees the key, and your server decides what's allowed: who may ask, how often, and what gets sent.

On most hosts, you save the key as a secret in the host's settings and your server code reads it from there. Your .env file stays on your laptop; it should never go live, and never into git.

On Toupas, a coding agent can do this for you: "add my OpenAI key to the live app as a secret". In apps made from a chat, it goes one step further. You add the key through a Toupas link, never in the chat, and Toupas adds it to each request itself. The app's code never sees the key at all, and the request can only go to the places the app lists.

What if each user has their own key?

Sometimes you want people to use their own AI account, so you don't pay for their usage. The same rule applies: the key shouldn't sit in the page or in the browser's storage. Save it on the server, readable only by the person who added it, and add it to requests there. We wrote up how to let users bring their own key.

Already leaked one?

Treat it as public, even if you deleted the commit.

  1. Revoke the key in the service's dashboard and make a new one.
  2. Put the new one in a secret on the server, as above.
  3. Check the service's usage page for anything you didn't do.
  4. Set a spending limit, where the service has one.

Removing the key from your code without revoking it isn't enough: old versions of the page, git history and caches keep the old one.

Tell your agent

"Find every API key or secret in the code that runs in the browser. Move each one to a secret on the server, and make the page call our server instead of the service."

Then search the built files yourself for the first few characters of your key. If it's there, it's public.

Questions

Can people see my API key in JavaScript?

Yes. Every file the browser runs can be read in its developer tools, including keys in requests the page sends. Minifying or encoding the key doesn't hide it.

Is it safe to put an API key in a .env file?

On your laptop, yes, as long as the file isn't committed. But values your build tool copies into the page, such as VITE_ or NEXT_PUBLIC_ ones, become public.

Where should I store API keys for a web app?

As a secret on the server, set in your host's settings. The page calls your server, and your server adds the key when it calls the service.

Are some API keys safe to put in the frontend?

Yes, keys the service says are publishable, such as a payment service's publishable key or a maps key restricted to your domain. If the docs don't say so, keep the key on the server.

Paste this into your coding agent

Make this app live with Toupas. Instructions: https://toupas.com/agents.md

Claude CodeCodexCursor

Keep reading