Gemini API Key Security: Keep Secrets out of the Browser
Before sharing a website that calls Gemini API, check where the key is stored and used. If the key is in code delivered to the browser, users may be able to read it even when it is not displayed on the page.

Keep API keys out of code delivered to browsers
Keep keys on the server rather than in the browser. Even with a variable named SECRET_KEY or hiding a button on the webpage. If the value is included in JavaScript delivered to users, it may be readable. The documentation Google on managing Gemini API Keys states that keys must not be embedded in production web or mobile apps or committed to Git.
A leaked key could allow others to use up the project's quota or create costs that the owner didn't intend. This risk is independent of the quality of the model's answers and does not disappear because the website has a login page.
How to call Gemini while keeping the API key on the server
Conceptual example of a one-page Q&A app:
- The user types a question on the web page. Then the website sends the request to Our app API
- The server checks user permissions, text length, request counts, and permitted task scope
- The server reads the key from an environment variable or secret store, then calls the Gemini API
- The server validates and limits returned data before responding to the webpage
In this sequence, the browser never needs the Gemini API key, and servers can record usage without unnecessary personal-text retention. This is Suggested architecture does not automatically certify system safety
For static sites, setting a key as a build-time environment variable is insufficient if the build tool inserts it into client-side code. The key will still be sent to users. Inspect the build output and where API calls actually execute. Minifying code or renaming variables only provides superficial concealment.
Check before making it available to others
- Separate frontend and backend: Check the build files delivered to users for keys or secrets, not just the source files
- Limited rights and use: Choose key types and restrictions using current documentation, enforce server-side request limits, and monitor quotas
- Prevent use on behalf of others: Authenticate callers instead of allowing uncontrolled, unlimited-length messages to a public endpoint
- Separate test data: Do not use actual client documents or personal data in development without checking processing terms
Google documentation checked October 29, 2026 distinguishes project-associated standard keys from authorization keys bound to service accounts. New AI Studio keys have defaulted to authorization keys since May 28, 2026, and unrestricted standard keys are rejected. Check your project’s key type and restrictions. Authorization keys remain secrets and do not replace app-user authorization.
Use key restrictions alongside user authorization. An unrestricted backend endpoint can consume quotas even without a key leak. Limit message size and requests per user, then test what happens at the limits. Billing alerts provide notice; the app still needs its own request controls.
If a key leaks, replace it and review its usage
Suppose a key is accidentally published in a public repository. Deleting it from the current file is insufficient because copies or revision history may remain. Google's documentation recommends creating a replacement, updating the app, testing it, then disabling or revoking the exposed key and checking API usage and billing. Troubleshooting Guide It also explains cases where a key is blocked by the system because a leak has been detected.
If a key leaks in production, an authorized administrator should follow organizational procedures and review whether other information was exposed too
What an API key does not protect against
Server-side keys reduce secret exposure but do not solve prompt injection, endpoint attacks, AI errors, or usage costs from real users. Provider key restrictions do not replace app authorization and request limits.
Decision criteria: If the site uses your Gemini API, provide a controlled backend endpoint before publishing. Without one, keep it a private prototype and never place secrets in shared files.
Sources
- Google AI for Developers: Using Gemini API keys
- Google AI for Developers: Troubleshooting Gemini API
- Google AI for Developers
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.