How to Store API Keys Safely: A Practical Guide for Developers and Vibe Coders
If you build with AI tools, you collect API keys fast: OpenAI, Claude, Gemini, Stripe, Supabase, Firebase, GitHub, Vercel. Most of them end up in the same places: a notes app, a chat with yourself, an old screenshot, or a .env file you copied between three laptops.
That works until a key leaks. Then it can mean a surprise bill, a hijacked account or leaked customer data. This guide covers where API keys should and shouldn't live, simple habits that prevent leaks, and what to do in the first ten minutes if a key is exposed.
Where API keys usually leak
- Committed to GitHub. A
.envfile or a hard-coded key gets pushed to a public repo. Bots scan public repos constantly, so assume a pushed key is found quickly. - Shipped in app or website code. Anything in your frontend or mobile app bundle can be extracted by anyone who downloads it.
- Screenshots and screen recordings. A key visible in a tutorial, a bug report or a livestream.
- Chats and notes. Keys pasted into Slack, Discord, email or a synced notes app, where they sit forever.
- Shared with AI tools. Pasted into a prompt "just to test something".
The rules that prevent most leaks
1. Never commit .env files
Add .env (and .env.local, .env.production) to your .gitignore before your first commit. Commit a .env.example with the variable names but no real values, so collaborators know what's needed.
2. Keep secret keys on the server
Secret keys belong in your backend or serverless functions, read from environment variables. Only keys designed to be public, such as a publishable Stripe key or a Supabase anon key protected by row-level security, should ever reach the browser or a mobile app.
3. Separate test and live keys
Use test or development keys while building and keep live keys for production only. If a test key leaks, the damage is limited.
4. Give keys the smallest possible access
Many services let you restrict a key to certain permissions, domains, IP addresses or projects. Use those settings. A read-only key that leaks is far less dangerous than an admin key.
5. Set spending limits and alerts
For AI and cloud APIs, set a monthly budget and usage alerts. If a key is abused, you'll know in hours, not when the bill arrives.
6. Rotate keys regularly
Replace important keys on a schedule, and always when someone leaves a project. Old keys you've forgotten about are the ones that cause trouble later.
7. Use a proper place to store them
Not a notes app, not a chat, not a screenshot. Use a password manager, your hosting platform's secret settings, or a dedicated key manager that encrypts keys and keeps them organized by project.
Quick checklist for every new project
- ☐
.envis in.gitignorebefore the first commit - ☐
.env.examplelists variable names with no values - ☐ Secret keys are only used on the server
- ☐ Test keys used in development, live keys only in production
- ☐ Key permissions restricted where the service allows it
- ☐ Spending limit and usage alerts set
- ☐ Keys stored somewhere encrypted, grouped by project
- ☐ Expiry or rotation date noted
What to do if a key leaks
Move fast and in this order:
- Revoke or rotate the key immediately in the service's dashboard. This is the only step that actually stops misuse.
- Update your app with the new key and redeploy.
- Check usage and billing for anything you don't recognize, and contact the provider's support if there was abuse.
- Find out how it leaked and fix that, whether it was a commit, a screenshot or a shared file.
- Don't rely on deleting the commit. Removing a key from Git history is good hygiene, but once it was public, treat it as compromised. Rotate first.
Tip: GitHub's secret scanning and push protection can block many known key formats before they're pushed. Turn them on for your repositories.
An easier way: an API key manager app for your iPhone
KeyDeck: API Key Manager is made for keys scattered across notes, chats and screenshots. It gives every project one tidy, encrypted place:
- Projects: group keys and logins by app or website
- Smart Paste: recognizes OpenAI, Claude, Stripe, Supabase, Firebase, GitHub and more, and fills in the
.envname for you - Live and Test keys told apart at a glance
- Notes, expiry dates and reminders before a key expires or gets old
- Safety checks that catch a public key saved as a secret, or the same key in two projects
- One-tap copy, and the clipboard clears itself after 60 seconds
- Send a project's
.envfile to your Mac with AirDrop, or import an existing.envfile in seconds - Key Checker to test whether a key still works, directly with the service
- Key history: replace a key and the old one is kept for 30 days, in case the new one breaks something
- Passwords too: a generator, strength scores, leak checks against known data breaches, and Password AutoFill
- Private by design: keys live in your iPhone's Keychain, protected by Face ID, with optional sync through your own iCloud. No account, no ads, no tracking, and revealed keys hide automatically while your screen is recorded or shared.
Download KeyDeck: API Key Manager on the App Store →
Disclosure: this app is made by the Everyday App Guides team. Learn more.
Related guides
Frequently asked questions
Is it safe to store API keys in a .env file?
On your own machine and on your server, yes, as long as the file is never committed to Git or shipped to users. A .env file is a way to load keys, not a backup or a safe place to keep them long term.
Can I put an API key in my mobile app or frontend?
Only if the key is designed to be public. Anything inside an app or website can be extracted. Call services that need a secret key from your own backend instead.
How often should I rotate API keys?
At least whenever someone leaves the project, whenever you suspect a leak, and on a regular schedule for important production keys. Many teams rotate every 90 days or so.
I deleted the commit with my key. Am I safe?
No. If it was ever public, assume it was copied. Revoke or rotate the key first, then clean up the history.
Is there an app to store API keys on iPhone?
Yes. KeyDeck stores API keys, tokens and passwords in your iPhone's Keychain behind Face ID, groups them by project, tells live and test keys apart, and can send a project's .env file to your Mac with AirDrop.
This article is general guidance. Always follow each service's own security recommendations.