Skip to content
VibecodeZero

How to keep API keys safe in a vibe-coded project

Updated

In this guide (9)
  1. Why a key needs protecting
  2. 1. Set a spending limit before you use the key
  3. 2. One key per tool and per project
  4. 3. Keep secret keys out of your app’s frontend code
  5. 4. Keep keys out of the AI chat
  6. 5. Store keys in environment variables or secrets
  7. 6. Keep keys out of git
  8. 7. If a key leaks: revoke, replace, check usage
  9. Your checklist

This guide works with any AI coding tool and any service that gives you an API key: an AI provider like OpenAI or Anthropic, a payment service, an email service, a database. It’s about the habits that keep a key from costing you money or exposing your app’s data.

The facts here come from OpenAI’s Best Practices for API Key Safety, Anthropic’s API key best practices and Why is Claude API usage billed separately, all checked October 21, 2026, Lovable’s docs on Secrets, checked October 27, 2026, Bolt’s help center on database secrets and Netlify’s docs on environment variables with frameworks, checked October 29, 2026, and Supabase’s docs on API keys and GitHub’s docs on removing sensitive data from a repository, checked October 30, 2026. Menus and names change, so check the provider’s page if something looks different. None of the links on this page are affiliate links.

Why a key needs protecting

An API key is a password-like code that lets a program use a service and bill your account for it. Anthropic’s help page compares it to a credit card number: if someone gets your key and uses it, they run up charges on your behalf. Some keys also open your data. Supabase’s secret key, for example, bypasses every Row Level Security rule in your database.

A leaked key keeps working until you delete it. So the goal is simple: make a leak unlikely, make it cheap if it happens, and know how to shut it down.

1. Set a spending limit before you use the key

Do this first, before the key goes into any tool. A limit that blocks requests caps what a leaked key can cost. An alert doesn’t: OpenAI says “Spend alerts alone don’t stop API traffic.”

  • Set a monthly limit with blocking at the provider. On OpenAI, that’s a monthly spend limit with hard enforcement. In the Claude Console, it’s Spend limits on the Billing page.
  • Keep auto-reload or auto-recharge off, or low, while you’re learning. They buy more credits automatically when your balance runs low, which is also how a leaked key keeps spending.

The step-by-step for each provider is in Set an API spending limit before you paste your key into any tool.

2. One key per tool and per project

Don’t reuse the same key everywhere. Anthropic recommends separate keys for different purposes, and Supabase says to use a separate secret key for each backend component. The reason is practical: if one key leaks, you delete that one, and everything else keeps working.

  • Name each key after the tool or project that uses it, like “lovable-recipes-app” or “cursor-laptop”. Then you know which one to delete.
  • In the Claude Console, a separate workspace per project also gets its own spend limit.
  • Only give a key to tools you trust. Anthropic says that when you give your key to a third-party tool, you give that tool’s developer access to your Claude Console account.

3. Keep secret keys out of your app’s frontend code

The frontend is the part of your app that runs in the visitor’s browser, and anyone can read it. OpenAI says never to deploy your key in browsers or mobile apps, and to send requests through your own backend instead.

What that looks like in practice:

  • Ask the AI to call the service from server code. For example: “Call the AI provider from a server function, and keep the key in Secrets.” The browser asks your server, and only your server holds the key.
  • Watch for public prefixes. Many frameworks put any variable with a certain prefix into the code sent to the browser. Lovable’s docs say that anything starting with VITE_ is a browser value, only for values that are safe to be public. Netlify’s docs mention prefixes like REACT_APP_ and NEXT_PUBLIC_, and say values embedded in the build can be read by anyone who inspects your app’s files. Never put a secret key in one of those.
  • Know which keys are meant to be public. Supabase’s publishable key (and the older anon key) is designed to be visible in your app, because it only reaches what your database rules (RLS) allow. Its secret key and the older service_role key are not: Supabase says never to put them in a browser, a shipped app or source control.

Lovable’s docs say secrets stored in frontend code “should be considered compromised”. If you’re on Lovable, step 4 of the Lovable security checklist for non-coders covers this in detail.

4. Keep keys out of the AI chat

It’s tempting to paste a key into the chat and say “use this”. Don’t. A chat is a conversation you send to a service. It isn’t a place to store passwords.

  • Store the key in the tool’s secrets setting and tell the AI its name. Lovable, for example, detects API keys pasted into its chat and guides you to store them in Secrets instead. In Bolt, the Secrets menu (database icon → Secrets) stores keys for server functions, so they never reach the browser.
  • Describe what you want, not the key. “I want to send emails with service X” is enough for the AI to set it up and ask you for the key in the right place.
  • Same rule for screenshots, support tickets and forums. Anthropic says not to share your key in support tickets, not even with Anthropic.

5. Store keys in environment variables or secrets

Instead of writing a key into your code, keep it in a setting that the code reads by name. OpenAI says never to commit your key to your repository and to use environment variables instead. GitHub’s docs say the same: avoid hardcoding secrets in code, and use environment variables or a secret management service.

  • In an AI app builder, that’s usually a Secrets menu, like the ones in Lovable and Bolt.
  • On your own computer, it’s often a file called .env, with lines like MY_SERVICE_KEY=.... The code reads the value by name.
  • On a host like Netlify, the host has its own environment variables screen. Remember the rule from step 3: a variable that the build puts into the browser isn’t secret.

6. Keep keys out of git

If your project is connected to GitHub, every file you commit is saved in its history, even after you delete it later.

  • Put .env in .gitignore. Anthropic says that if you keep keys in a .env file, you must add it to your .gitignore so it’s never shared by accident. GitHub’s docs also say to add files that shouldn’t be tracked to .gitignore.
  • Turn on push protection on GitHub. GitHub’s docs suggest enabling it so pushes that contain hardcoded secrets are detected and blocked.
  • A public repository is public. Anyone can read it, history included.

To check a project, paste this into your AI tool. It only looks, and never shows a full key:

I want to check that no API keys or secret keys are exposed in this project.

Don't change, move or delete anything. Only look and report back.
1. Find every place where an API key or secret key is used: code files, .env files, environment variables and config files.
2. For each one, tell me which service it belongs to, showing only its last 4 characters. Never print a full key.
3. Tell me if any key is in code that runs in the browser, in a variable with a public prefix (like VITE_, NEXT_PUBLIC_ or REACT_APP_), or written directly in the code.
4. Tell me if any .env file is missing from .gitignore, or if a key was ever committed to git.
Explain what you found in plain English, then wait for me before changing anything.

7. If a key leaks: revoke, replace, check usage

A key has leaked if it was in browser code, in a public repository, in a screenshot, in a chat you shared, or in a tool that reported a breach. When in doubt, treat it as leaked.

  1. Revoke or rotate it at the provider, right away. GitHub’s docs say this is the first step, and that once a secret is revoked or rotated it can no longer be used. Deleting the key from your code doesn’t stop it: GitHub says commits with the key may still be accessible in clones or forks.
    • Claude Console: on the API keys page, click the three dots next to the key and select Delete API key.
    • OpenAI: rotate the key from the API Keys page.
    • Supabase: rotate the secret key. Supabase’s guide Rotate a leaked or compromised API key has the steps.
  2. Create a new key and put it in every tool that used the old one, through Secrets or environment variables, not in the code. This is where one key per tool pays off.
  3. Check the usage. OpenAI says to check its Usage page, and Anthropic recommends regularly reviewing the Logs and Usage pages. Look for usage you don’t recognize.
  4. Fix how it leaked, so the new key doesn’t follow the same path. Supabase’s docs tie the two together: rotate the key as soon as you fix the cause of the leak.

If you see charges you didn’t make, contact the provider’s support from your account. We can’t promise a refund, because each company decides. For a real case, with Claude, OpenAI and Gemini keys, see OpenCode breach: what to do with your AI API key.

Your checklist

  • A spending limit that blocks requests is set at the provider.
  • Auto-reload or auto-recharge is off, or set low.
  • Each tool and project has its own key, with a name you’ll recognize.
  • No secret key is in browser code or in a variable with a public prefix.
  • Keys live in Secrets or environment variables, not in the code and not in the chat.
  • .env is in .gitignore, and no key was ever committed.
  • You know where to delete each key, and where its usage page is.

To check your app for other data it might be exposing, see Could my app be exposing personal data?, and for a full leak self-check, including database rules and public storage buckets, see Is my vibe-coded app leaking?. For the other checks worth doing on a live app, see I launched my vibe-coded app. Now what?

Questions people ask

I am on a monthly subscription. Can a leaked key still cost me money?

It depends on what leaked. A flat monthly subscription that you use by signing in, like a Claude Pro plan, isn't an API key. An API key belongs to an API account and bills each use to that account, which is billed separately from the subscription. Anthropic, for example, says paid Claude plans and the Claude Console are separate products and the Claude API is billed separately. So if you created an API key at some point, a leaked key can spend whatever its API account allows, like its credit balance, auto-reload or the card on file, until you delete it or it hits a blocking limit. If you never created a key, check that no tool is using one without you noticing: if an ANTHROPIC_API_KEY environment variable is set on your computer, Claude Code uses it instead of your plan and you pay API rates.

Is it safe to paste my API key into the AI chat?

Don't. The chat is a conversation you send to a service, not a safe. Store the key in your tool's Secrets or in an environment variable and refer to it by name. Lovable, for example, detects API keys pasted into its chat and guides you to store them in Secrets instead. Anthropic says not to share a key in support tickets either, not even with Anthropic.

I deleted the key from my code. Am I safe now?

Not yet. Revoke or rotate the key at the provider first. GitHub's docs say that is the first step, and that once a secret is revoked or rotated it can no longer be used. Removing it from your code later doesn't remove it from copies: GitHub says commits with sensitive data may still be accessible in clones or forks of your repository.

Is the Supabase anon or publishable key a secret?

No. Supabase's docs say publishable keys, and the older anon key, are designed to be public and safe to put in a web page or app, because they only reach what Row Level Security allows. The secret key and the older service_role key are different: Supabase says a secret key bypasses every Row Level Security policy, and must never go in a browser, a shipped app or source control.

Does a spend alert protect me if a key leaks?

No. An alert only tells you. OpenAI says spend alerts alone don't stop API traffic, so set a monthly limit with hard enforcement. A limit can be slightly exceeded, because enforcement isn't instant, but it keeps a leaked key from spending without end. Our page Set an API spending limit shows where to set one on OpenAI, the Claude Console and OpenCode Console.

How do I know if my key was used by someone else?

Check the provider's usage pages. OpenAI says to check its Usage page to see whether the usage matches your own work, and Anthropic recommends regularly reviewing the Logs and Usage pages in the Claude Console. Usage you don't recognize, or charges you didn't make, mean you should delete the key right away and create a new one.

New guides by email. An email when we publish a new guide, error fix or chapter, in plain English. Subscribe