← All posts
api key leaks prevention vibe codingvibe coding securityprevent API key leaksAI app security

API Key Leaks Prevention in Vibe-Coded Apps: Complete Guide

O

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:

  • [ ] Scan your entire codebase for hardcoded secrets using a secret leak scanner. AI-generated code often pastes keys directly into source files.
  • [ ] Check your `.gitignore` — does it include .env, .env.local, .env.*? Many AI starters forget this.
  • [ ] Audit your client-side code — open your browser's DevTools Network tab and look for API keys in request headers or URLs. Vibe-coded apps often leak keys to the frontend.
  • [ ] Review environment variable usage in your deployment platform (Vercel, Netlify, Railway). Are secrets scoped to production only?
  • [ ] Test your build output — run 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-recommend

    Then 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:

  • Prefixing client-side vars with `NEXT_PUBLIC_` — This exposes them in the browser bundle. Only use NEXT_PUBLIC_ for truly public values (like Google Maps API key with referrer restrictions).
  • Committing `.env` files — AI coding tools often generate a .env.example but forget .gitignore. Always check that .env* is ignored.
  • Hardcoding test keys in examples — When asking ChatGPT to generate code, it often includes placeholder keys like sk-test-... that end up in production code.
  • Using the same key for dev and prod — A leaked dev key (e.g., in a screenshot or debug log) can be used against your production account if scopes aren’t restricted.
  • Ignoring build artifacts — Some frameworks (like Next.js) generate static files that include environment variables if you accidentally use getServerSideProps with inline keys.
  • Why vibe-coded apps are especially vulnerable

    AI coding tools prioritize speed over security. They often:

  • Suggest hardcoded configuration for simplicity
  • Generate client-side code that calls APIs directly
  • Omit environment variable boilerplate
  • Forget to add .gitignore entries
  • This 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.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free