Why It Matters for AI-Coded Apps
AI coding tools are notorious for hardcoding API keys directly in source files. When prompted to ‘connect to the Stripe API,’ LLMs will often embed the key directly in the code rather than reading from environment variables. 38% of vibe-coded apps in our research exposed at least one API key in client-side bundles.
Real-World Example
AI-generated code includes const stripe = new Stripe('sk_live_abc123...') directly in a React component. This key ships in the JavaScript bundle visible to anyone who opens browser DevTools. Within hours, bots scanning public bundles find the key and start making charges.
How to Detect and Prevent It
Never hardcode secrets in source code. Use environment variables and .env files (add .env to .gitignore). Use server-side API routes to proxy requests that need secret keys. Implement secret scanning in CI/CD (GitGuardian, TruffleHog). Rotate keys immediately if exposed.
Frequently Asked Questions
What should I do if I accidentally committed an API key?
git filter-branch or BFG Repo Cleaner to purge it from history if the repo is public. Assume the key was compromised the moment it was pushed.What is the difference between public and secret API keys?
pk_live_) are designed to be used in client-side code and have limited permissions. Secret keys (like sk_live_) must never be exposed to the client and have full API access. AI tools often confuse these and put secret keys in frontend code.How do I use environment variables in Next.js?
.env.local file with your keys. Variables prefixed with NEXT_PUBLIC_ are exposed to the browser (use for public keys only). Server-only variables (no prefix) are only available in API routes and server components. Never prefix secret keys with NEXT_PUBLIC_.