AIDiscount优汇智
Home / Blog

API Key Security: Storing, Scoping and Rotating Keys Safely

Practical rules for handling AI API keys: environment variables, never shipping a key to the browser, per-project keys, and what to do after a leak.

2026-11-25 · 3 min read · AIDiscount team

API keysSecurityBest practices

An API key is a bearer credential: whoever holds it can spend against your balance. Treat it like a password, not like a config value that is safe to leave lying around.

Store keys as environment variables, not in code

Never write a key as a literal string in a source file. Read it from the environment instead:

# Python
import os
api_key = os.environ["AIDISCOUNT_API_KEY"]

# Node.js
const apiKey = process.env.AIDISCOUNT_API_KEY;

For local development, put the variable in a .env file and add .env to .gitignore before creating the file, so a tired git add . never catches it. In CI/CD and hosting platforms, set the key through the platform's secrets feature (GitHub Actions secrets, your host's environment variable settings), never in a committed workflow file or Dockerfile.

Never put a key in frontend or mobile code

Anything shipped to a browser or a mobile app is public, no matter how it is obfuscated or minified. A key embedded in client-side JavaScript, a mobile app binary, or a browser extension will be extracted within hours of release; this is routinely automated by scrapers that specifically scan public repositories and shipped app bundles for key-shaped strings. If a feature needs an AI call from the browser, add a small backend endpoint that holds the key server-side and forwards the request, so the browser only ever talks to your own server.

Use a separate key per project or environment

One key for everything means one leak affects everything, and a spike in usage from a single key doesn't tell you which app caused it. Create a separate key for each application, and separate keys for development, staging and production, from the API Keys page in the dashboard. If a leak happens, you can revoke the one affected key without breaking everything else, and the Usage page shows you exactly which key is spending.

Give each key only what it needs

If a key only needs a subset of models or a lower rate limit, scope it that way instead of issuing a key with full access everywhere. A narrower key limits how much damage a leak can do.

What happens when a key leaks

Leaked keys are found quickly, mostly by automated scanners, not by someone manually reading your code: public GitHub repositories, pastebins, browser extension source, client-side JavaScript bundles, and even screenshots or screen recordings posted online are all scraped continuously for key-shaped strings. Once found, a key is typically abused within minutes for automated bulk usage, run until the balance is exhausted or the key is revoked.

If you suspect a key leaked:

  1. Revoke it immediately from the API Keys page. This takes effect right away.
  2. Create a new key and update every place the old one was used.
  3. Check the Usage page for the time window to see what was actually run on it.
  4. Find how it leaked before you consider it resolved: search your git history (not just the latest commit; git log -p past commits can still contain a key you later removed), check CI logs, and check any public repository or gist.

A key already committed to git history is not fixed by deleting the line in a new commit; the old commit still contains it. Either rewrite history (git filter-repo or BFG Repo-Cleaner) on a repository you control, or treat the key as burned and rotate it, which is usually faster and safer than rewriting history on a shared repository.

Rotate keys on a schedule, not only after a leak

Rotating a key periodically, even with no known leak, limits how long a key that leaked silently (and was never noticed) stays useful to whoever has it. Issue the new key, update your app, confirm it works, then revoke the old one, rather than revoking first and debugging an outage under pressure.

Checklist

  • Key lives in an environment variable or secrets manager, never in a committed file.
  • .env is in .gitignore before the file is created.
  • No key appears in any frontend, mobile, or browser extension code.
  • Separate keys per app and per environment (dev/staging/production).
  • A plan exists for "what do I do if this leaks", written down before it happens, not improvised afterward.
Try it with AIDiscount. One API key works with OpenAI- and Anthropic-compatible tools. Pay as you go, no monthly fee.
Create a free accountRead the API docs

Related guides

Languages

English · 简体中文 · 繁體中文 · 日本語 · 한국어 · Español · Français · Deutsch · Português · Русский