← All posts
api keysvibe codingsecuritysecretsthird-party

How to Secure Third-Party API Keys in Vibe-Coded Apps

O

OverMCP Team

Quick answer

To secure third-party API keys in vibe-coded apps, you must never hardcode keys in client-side code, use environment variables with a secure vault, and regularly scan your codebase for leaked secrets. Automated tools like OverMCP can detect exposed keys before they cause damage.

What to check first

Before you deploy your vibe-coded app, run through this checklist:

  • [ ] Are all API keys stored in environment variables (not hardcoded in source files)?
  • [ ] Are your .env files added to .gitignore and never committed to version control?
  • [ ] Do you use a secrets manager (e.g., Vercel Environment Variables, AWS Secrets Manager) for production?
  • [ ] Have you scanned your GitHub repo for any leaked keys using a secret leak scanner?
  • [ ] Are API keys scoped to minimum necessary permissions (read-only if possible)?
  • [ ] Do you rotate keys regularly and after any suspected leak?
  • [ ] Are keys excluded from client-side bundles (e.g., Next.js public runtime config)?
  • Step-by-step fix

    1. Move hardcoded keys to environment variables

    AI tools like Cursor often generate code with hardcoded keys. Replace them immediately:

    // ❌ Bad: hardcoded in source
    const stripeKey = 'sk_live_...';
    
    // ✅ Good: read from environment
    const stripeKey = process.env.STRIPE_SECRET_KEY;

    In Next.js, use server-only environment variables for secret keys by prefixing with NEXT_PUBLIC_ only for public data. Never expose secret keys to the browser.

    2. Add .env to .gitignore

    Ensure .env and .env.local are in .gitignore:

    # .gitignore
    .env
    .env.local
    .env.*.local

    3. Use a secrets manager for production

    For Vercel deployments, set environment variables in the Vercel dashboard or use vercel env add. For other platforms, use the equivalent secure store.

    4. Scan your codebase for leaks

    Run a secret leak scanner to find any keys that might have been accidentally committed. This can be integrated into your CI/CD pipeline for continuous monitoring.

    5. Rotate any compromised keys

    If you find a leaked key, immediately revoke it and generate a new one. Update your environment variables and redeploy.

    Common mistakes

    Vibe-coded apps often suffer from these errors:

  • Hardcoding keys in client-side code: AI models sometimes place API keys directly in React components or utility files that end up in the browser bundle. Anyone can view your source and steal the key.
  • Committing .env files: A single git add . without a proper .gitignore can commit your secrets to the repo forever.
  • Over-permissioned keys: Developers often generate API keys with full access when read-only would suffice. If leaked, the damage is greater.
  • No rotation policy: Keys stay unchanged for months, increasing risk if leaked.
  • Using public environment variables for secrets: In Next.js, NEXT_PUBLIC_* variables are inlined into client bundles. Never use them for secret keys.
  • FAQ

    How do I find leaked API keys in my vibe-coded app?

    Use a dedicated secret leak scanner that checks your codebase, Git history, and dependencies for patterns matching common API key formats.

    Can I store API keys in a .env file in production?

    No. For production, use your hosting platform's environment variable manager or a dedicated secrets management service. Never rely solely on .env files in deployed environments.

    How often should I rotate third-party API keys?

    Rotate keys at least every 90 days, and immediately after any suspected leak or when a developer leaves your team.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free