Gemini API Key Security: Keep Secrets out of the Browser

Your page may never display the API key. If it ships in JavaScript users download, though, they may still read it. Trace the key from build output to the Gemini API call before launching.

A key in a server cabinet separate from a browser window represents server-side secret storage

Keep API keys out of code delivered to browsers

Keep the key on the server. A variable named SECRET_KEY or a hidden button will not help if the build embeds the value in user-facing JavaScript. The key can still be read. Google’s Gemini API key documentation warns against embedding keys in published websites or mobile apps and committing them to Git.

Someone who gets the key could consume the project’s quota or cause unexpected charges. A website login does not fix an exposed key, and this risk is separate from whether the AI answers correctly.

How to call Gemini while keeping the API key on the server

Consider a single-page question-answering app. The request flow should look like this:

  1. The browser sends the question to Our app API rather than directly to Gemini with the owner’s key.
  2. The server checks authorization, message size, request count and permitted operations.
  3. The server reads the key from an environment variable or secret store, then calls the Gemini API.
  4. The server checks the response and limits what goes back to the browser.

The browser never needs the Gemini API key. The server can also record usage without retaining more personal content than necessary. This is a recommended architecture not a certification that the app is secure.

Static websites need extra care: an environment variable at build time does not mean a key stays on the server. If the build inserts its value into client files, it leaks. Inspect the files actually delivered and where the API call runs. Minification or renaming cannot protect the key.

Check before making it available to others

Google’s documentation reviewed on October 29, 2026 distinguishes project-bound standard keys from service-account-bound authorization keys. It states that new AI Studio keys became authorization keys on May 28, 2026 and unrestricted standard keys are rejected. Verify your actual project’s key type and restrictions. An authorization key is still a secret; do not embed it in a browser or use it instead of user authorization.

Even with the key on the server, an unrestricted public endpoint can exhaust quota. Set limits on message size and requests per user for the task, and test over-limit behavior. Billing alerts help detect an incident but do not replace request controls.

If a key leaks, replace it and review its usage

If a key lands in a public repository, removing it from the latest file is not enough: copies and history may remain. Following Google’s guidance, create a replacement, switch and test the app, then disable or revoke the exposed key. Review API usage and charges. The Troubleshooting Guide explains cases in which a leaked key is blocked by the system.

For a production incident, have an authorized administrator follow the organization’s process and check whether other information was exposed.

What an API key does not protect against

Server-side storage reduces exposure, but does not stop prompt injection, endpoint attacks, wrong AI answers or costs caused by legitimate users. Provider key restrictions do not replace app authorization and request limits.

If the site uses the owner’s Gemini API account, put a controlled server-side call path in place before public launch. Without one, keep the project a private prototype and never put the secret key in files you share.

Read moreGoogle AI Studio Apps: Who Pays for Usage?Read who pays API costs and how to estimate costs when opening your app to users

Sources

Google key-type and standard-key restriction requirements reviewed October 29, 2026. This explains documented architecture without inspecting a production system or creating real keys. Requirements may change.