Where should an AI API key live?
A practical guide to keeping credentials out of browsers, repositories and routine logs.

The browser is not a secret store
A key embedded in frontend code can be seen by anyone who loads the application, even if it comes from a build-time environment variable. Keep privileged API credentials on a server you control. The browser should call your authenticated backend, which checks the user's permissions before making the upstream request.
Map the path: a browser calls your backend, the backend reads a key and sends the API request, then returns only the needed result. A key should not appear in HTML, frontend scripts, public mobile configuration or a URL query string. HTTPS protects transport, but a person receiving client code may still inspect its contents; encryption does not make a public client a secret store.
Give each environment its own key
Use separate credentials for development and production, and separate applications or teams where the platform supports it. Store secrets in environment configuration or a dedicated secrets manager; never commit them to a repository or paste them into an issue. Grant only the access and quota each workload needs.
Use separate keys for development, testing and production; split further by application or team where supported. If one key leaks, its impact is easier to identify and it can be revoked independently. Apply available permissions, quotas and access restrictions rather than sharing a powerful key across unrelated services. Record owners and purposes without storing plaintext keys in the register.
Keep API credentials protected and separate from public application code.Make accidental exposure recoverable
If a key appears in a screenshot, log or public commit, treat it as compromised. Revoke or rotate it, deploy the replacement and inspect relevant usage. Removing the text from the latest commit is not enough if the old value remains in history. Have a way to reach the people responsible for rotation before an incident happens.
When a key leaks, restrict or revoke it, create and deploy a replacement, and inspect related usage and access records. Rehearsing rotation in advance reduces confusion during an incident. Never paste the replacement into chat, tickets or screenshots. If a service cannot switch immediately, assign an owner and a transition plan, then monitor until the old credential is no longer used.
Check the edges too
Redact authorization headers and credentials from logs, error reports and support messages. Review proxy configuration, CI output and third-party integrations. A concise runbook and a periodic access review are often more useful than a long policy nobody uses. Follow your platform's current key-management documentation for available controls.
Inspect build artifacts, CI output, error monitoring, proxy logs and screenshots as well as source code. Authorization headers, full request bodies and environment variables can be printed during troubleshooting. Redact logs and retain only what is needed. For public support, a request ID, time and sanitized error are usually enough; a leaked repository secret may remain in history or caches.
People, applications and environments change, so permissions that were reasonable can become unnecessary. Review unused keys, broad access and unusual usage, and revoke credentials no longer needed. Controls depend on what the platform actually supports. Account security, server permissions and dependency management matter too; hiding a string alone is not a complete security plan.
Find where credentials can appear
Look for credentials in repositories, deployment configuration, CI logs, support tickets, screenshots and browser traffic. A frontend environment variable is not secret if its value is bundled into code sent to the browser. Mobile and desktop clients can also be inspected, so they are poor places for a long-lived server key.
Have clients call your backend; authenticate users, validate inputs and apply available limits there before reading credentials from environment configuration or a secrets manager. Avoid keys in URL query strings, browser code and error messages. Use separate credentials for development, testing and production, with the narrowest available permissions and limits.
Prepare a practical rotation procedure
Identify who can create and deactivate a key, where it is read and which services depend on it. Deliver replacement credentials through a secure configuration channel, verify a minimal request, then deactivate the old key according to the platform's capabilities. Don't paste either key into tickets, group chats or logs.
If a key is exposed, restrict or revoke it promptly, inspect usage and update dependent services. Then investigate the source. Removing a line from a repository is not enough because history or caches may retain it. Rehearse with test credentials and document the process without recording actual secrets.
Make protection part of everyday work
Give credentials recognizable purposes and owners. Supply them only to the environments that need them, remove unused access and respond to scanner findings rather than treating scanning as a substitute for rotation and least privilege.
Monitor unusual usage and authentication failures without putting full keys in alerts. Check frontend builds and screenshots before release, and verify that a revoked test key no longer works. Give third-party integrations separate credentials where possible so access can be withdrawn without rotating unrelated services.
A small release checklist
Before deployment, search the frontend bundle and repository history for accidental credentials, verify that production uses its own key, and send one small request from the backend. Confirm that an error report hides authorization headers. If a key has been exposed, rotate it first and check usage rather than relying only on deleting the visible text.
For each service, decide who can create and revoke keys, who deploys replacements and who investigates unusual usage. Update the record and remove access during handovers. Use test credentials in exercises rather than copying production secrets into demonstration material, and make sure emergency contacts remain current.