How Do API Keys Actually Leak? (It Is Almost Never .env)
Every leaked-key post gets the same advice: put it in .env, gitignore it, done. We went through ten real incidents from this year. Not one leaked through .env.
Keys leak through the places .env hygiene does not cover: application logs, client-side bundles, error trackers, browser extensions, public repos with history, and AI agents that echo secrets into transcripts. The fix is not better storage, it is blast radius: scoped keys, short-lived credentials, spend alerts, and automatic rotation. Assume any key that has ever been logged or bundled is burned.
Going through the leaked-key stories of the past year, bills ranged from $2 to about $87K.
In This Guide
Ten incidents, one pattern
Blast radius beats hygiene
The 30-minute audit
4 Tips to Get Started
Set a hard spend limit on every AI and cloud API key, not an alert, a limit. The difference between a $2 leak and an $87K leak is usually whether the provider let the meter keep running.
Scan your logs and error tracker for key-shaped strings (sk-, AKIA, ghp_, eyJ). Keys end up in logs through debug statements and exception payloads far more often than through commits.
Give each integration its own key with minimum scope. One key used by frontend, backend and cron means one leak burns everything.
Rotate on a schedule and on suspicion. If a key is older than 90 days and cannot be rotated without an incident, that dependency is the actual finding.
Ready to Stop Worrying About Infra?
Knowledge is the first step. Get a free 1-hour diagnostic and see where your infrastructure leaks time, money, and focus.
Sources & References
roqy.io cites authoritative sources including engineering blogs, vendor documentation, industry surveys, and academic research to ensure accuracy.
Related Guides
Who Owns Vibe-Coded Internal Apps When the Builder Leaves?
Marketing built a dashboard on a personal Vercel account with a service key. The builder left. Who owns it, who fixes it, and how to prevent the next orphaned app.
What Do You Do When Your DevOps Team Shrinks but Your Infrastructure Triples?
Microservices tripled in 8 months, the DevOps team stayed the same size, then got smaller. The realistic options: platform guardrails, deletion, and buying back hours.
Why Do Internal Dashboards Become a Maintenance Nightmare, and How to Fix It?
Tens of internal tools, each breaking when the data model changes. Why internal dashboards rot, which ones to keep, and the pattern that makes the rest maintainable.
Why Do AI-Generated PRs Break Your Infrastructure, and What Guardrails Work?
AI pull requests change pipelines and add services that need config outside the codebase. Why code review cannot catch it, and the four guardrails that do.
Ron Cohen — DevOps engineer & founder, roqy.io
Content reviewed by the roqy.io team. Our guides are based on publicly available data and cited sources above.
Disclaimer: This content is for informational and educational purposes only. It does not constitute professional advice or a guarantee of specific results. Infrastructure needs vary by team, stack, and stage. Consult a qualified engineer for advice tailored to your situation.
Frequently Asked Questions
What are the most common ways API keys leak?
In order of frequency in real incidents: application and server logs, client-side JavaScript bundles that shipped the key to the browser, error-tracking services capturing exception payloads, public Git repositories (including deleted commits that stay in history), browser extensions and packages exfiltrating secrets, and AI coding agents that paste secrets into prompts or transcripts.
Is putting secrets in .env files safe?
.env is fine as a local convention. It is not a security boundary. It does nothing for keys that reach logs, bundles, error trackers or a teammate's screenshot. Treat .env as where keys rest, not where they are protected.
How do I detect a leaked key before the bill arrives?
Three layers: provider-side spend limits that hard-stop runaway usage, your own alerting on abnormal API call volume or geographic anomalies, and secret scanning (GitHub push protection, gitleaks in CI) on every repo including private ones. The spend limit is the only one that caps the damage with certainty.
Get the DevOps Ecosystem Behind You
One accountable engineer. Full platform behind him. Month-to-month, no lock-in.