API Key Leaks Prevention in Vibe-Coded Apps: Complete Guide
OverMCP Team
Quick answer
Prevent api key leaks in vibe-coded apps by never hardcoding secrets, using environment variables with strict access controls, and running automated secret scanners before every deploy. Most AI coding tools generate code that accidentally exposes API keys in client-side bundles or public repos — a simple pre-deploy scan can catch 90% of these leaks before they cause damage.
What to check first
Before diving into fixes, run this checklist on your vibe-coded app:
.env, .env.local, .env.*? Many AI starters forget this.npm run build and inspect the generated JS files for any leaked strings.Step-by-step fix
1. Move all secrets to environment variables
Replace any hardcoded API keys with process.env.YOUR_KEY. For example, in a Next.js app built with Cursor:
// ❌ Bad: hardcoded in component
const openai = new OpenAI({ apiKey: 'sk-...' });
// ✅ Good: from environment variable
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });2. Set up environment variables correctly
Create a .env.local file (never commit it):
# .env.local (add to .gitignore!)
OPENAI_API_KEY=sk-...
STRIPE_SECRET_KEY=sk_live_...Then in your deployment platform (e.g., Vercel), add the same variables in the project settings. Use different values for development and production.
3. Add pre-commit hooks to block leaks
Install a tool like secretlint or use OverMCP’s continuous security monitoring to catch secrets before they reach GitHub:
npm install -D secretlint @secretlint/secretlint-rule-preset-recommendThen add a .secretlintrc.json:
{
"rules": [
{
"id": "@secretlint/secretlint-rule-preset-recommend"
}
]
}And a pre-commit hook in package.json:
"scripts": {
"secretlint": "secretlint '**/*'"
},
"lint-staged": {
"*.{js,ts,jsx,tsx,json,yaml}": ["secretlint"]
}4. Never expose keys on the client
AI tools like v0 or Lovable sometimes generate code that calls backend APIs directly from the browser. Always route through a serverless function or backend endpoint:
// ❌ Bad: calling OpenAI from browser
const res = await fetch('https://api.openai.com/v1/...', {
headers: { 'Authorization': `Bearer ${process.env.NEXT_PUBLIC_OPENAI_KEY}` }
});
// ✅ Good: use a Next.js API route
// pages/api/chat.js
export default async function handler(req, res) {
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const completion = await openai.chat.completions.create({ ... });
res.json(completion);
}5. Scan your repo regularly
Use a tool like OverMCP’s free AI app security scanner to automatically find leaked keys in your codebase, including in commit history.
Common mistakes
Even experienced devs make these errors when vibe-coding:
NEXT_PUBLIC_ for truly public values (like Google Maps API key with referrer restrictions)..env.example but forget .gitignore. Always check that .env* is ignored.sk-test-... that end up in production code.getServerSideProps with inline keys.Why vibe-coded apps are especially vulnerable
AI coding tools prioritize speed over security. They often:
.gitignore entriesThis makes api key leaks prevention vibe coding a critical skill for anyone using these tools. A single leaked Stripe or OpenAI key can cost thousands — or lead to account takeover.
How OverMCP helps
Instead of manually reviewing every AI-generated file, you can connect your Vercel or GitHub repo to OverMCP and get automatic alerts whenever a new secret leak is detected. The Vercel security scanner checks every deployment before it goes live, so you never ship an exposed key again.
FAQ
What is the most common way API keys leak in vibe-coded apps?
The most common leak is hardcoding the key in a frontend component or API route file. AI tools often paste the key directly into source code when you ask them to integrate a service like OpenAI or Stripe.
Can I use environment variables safely in a static site?
Yes, but only for server-side code. If your site is fully static (no server), you cannot safely use secret keys. You need a backend proxy or serverless function to keep keys out of the client bundle.
How often should I scan my repo for leaked secrets?
Scan on every commit and before every deploy. Use automated tools like OverMCP’s continuous monitoring to get real-time alerts without manual effort.