Keep Your API Keys and Secrets Safe with Claude Code
TL;DR
A funded API key sitting behind an endpoint with no auth and open CORS is an open faucet to the whole internet - one real leak cost over 600 dollars in a single night. Keeping keys safe is not complicated, but it is non-negotiable. This is the checklist: keys stay server-side, spending endpoints get auth and rate limits and hard caps, and auto-reload never counts as a safety net.
Why this is the one mistake you cannot afford
Most bugs cost you time. A leaked or unguarded API key costs you money, in real time, while you sleep. A key that can spend - Anthropic, OpenAI, fal, Stripe - sitting behind an endpoint with no authentication and open CORS is reachable by anyone who learns the URL. They pick the model, they pick the volume, and your card pays for it. This has happened for real and it drained over 600 dollars overnight before anyone noticed.
The safe-key checklist
- 1Keys live server-side only. Never in client code, never in a public repo, never in the browser bundle. Load them from an environment file that is gitignored.
- 2Never commit secrets. Add .env and .env.local to .gitignore before your first commit, and ask Claude to check nothing sensitive is staged.
- 3Any endpoint that spends money needs authentication or a shared secret. No open, unauthenticated endpoint should ever be able to reach a funded key.
- 4Lock the origin allowlist. Never ship CORS set to a wildcard on a spending endpoint - that opens it to the entire internet.
- 5Add rate limiting and a hard cost cap. Cap the model and max tokens (or the equivalent ceiling), and ideally a per-day spend limit, so a leak is bounded instead of unlimited.
- 6Turn OFF auto-reload on funded keys. Auto-buy credits turns a small leak into unbounded spend. It is the opposite of a safety net.
Have Claude audit it for you
Before anything that touches a key goes live, paste this into Claude Code and let it check the surface for you.
markdownAudit this project for API-key exposure. Check:
1. Are any keys hardcoded in source or committed to git?
2. Is .env / .env.local in .gitignore?
3. Does any endpoint that uses a paid key lack auth?
4. Is CORS set to a wildcard anywhere on a spending route?
5. Is there rate limiting and a max-tokens / cost cap?
List every issue by severity with the file and line. Do
not change anything - just report.Common questions
I only need this for a quick demo. Do the rules still apply?
Yes. Open CORS plus no auth plus a funded key is never acceptable, not even for a demo or beta. The internet finds unguarded URLs faster than you expect, and the demo is exactly when auto-reload quietly does the damage.
Is putting the key on the server enough?
No. Server-side is necessary but not sufficient. An unauthenticated proxy is as exposed as a leaked key. You still need auth, an origin allowlist, rate limiting, and a cost cap on top.
Why turn off auto-reload?
Auto-reload turns a bounded problem into an unbounded one. If a key leaks, a fixed balance limits the damage; auto-reload keeps buying more credits for the attacker until you notice. Cap the spend instead.
How do I know if my current setup is safe?
Run the audit prompt above in Claude Code. It checks for hardcoded keys, gitignore gaps, unauthenticated spending endpoints, wildcard CORS, and missing caps, and reports each with the file and line.
Want the guardrails baked into your setup from day one?
Get the other 17 in the starter stack, plus 5,000+ builders - $9/mo, cancel anytime.
Join the Club