How to keep API keys safe when you code with AI agents
Where keys leak once an agent helps you build, what to do instead, and a prompt that checks your own project.
Written by the IndieHacked teamSecurity8 min read
Keep each API key in a git-ignored env file on your computer and in your host's environment variables in production, and refer to it everywhere else by name.
Most leaks now start in what an agent records: a key typed into a command it saves, an agent settings file that gets committed, or a public variable built into your app.
Run the prompt below to see where your project stands, then give your agent a SECRETS.md to follow with our free Secrets setup.
Set up the rules for your stack:Secrets setup
Give every key one home
A key that lives in one known place can be rotated in one place. On your computer that is a .env file your repository ignores. In production it is your host's environment variables, in CI the CI's secret store, and in a mobile build the build service's own variables. Your code, your commands and your notes refer to the key by its variable name, such as STRIPE_SECRET_KEY, and never hold the value.
Write that list down where your agent will read it. An agent starts each session without the last one's lessons, so a rule that only lives in a chat is gone by the next morning.
Mistake: typing a key into a command
Ask an agent to check a database and the command it proposes carries the password from your config. Approve it with Claude Code's "Yes, and don't ask again" and the whole command, password included, is saved as a rule in .claude/settings.local.json. Apiiro found 1,122 public copies of that file with a real secret in them in November 2026, a story we covered in the news. A command line is also kept in your shell history and shows in process listings while it runs.
- Instead: reference the key by name, so the command reads $DATABASE_URL and the rule records the name, not the password.
- Approve patterns, such as npm run *, instead of one literal command.
- Open your agent's settings file now and delete every allow rule that holds a password, a token or a connection string.
Mistake: committing what your agent writes
Agents keep files next to your code: approvals, chat exports, memory files and handoff notes. Claude Code keeps its local settings file out of git through your global git excludes when it creates it, but that rule stays on your computer. It doesn't cover a folder that becomes a repository later, a clone on another machine, or a file already committed. Apiiro found real secrets in shared .claude/settings.json files, in exported agent chats and in agent memory files too.
- Instead: add .env files, .claude/settings.local.json and your agent's chat exports to the .gitignore in your repository, so the rule travels with the code.
- A file that is already tracked ignores .gitignore. Untrack it with git rm --cached and the path, then commit.
- Treat agent folders like .env files when you zip, publish or copy a project.
Mistake: a secret in a public variable
Frameworks build some variables into the code your visitors download. Next.js does it with every NEXT_PUBLIC_ variable, Vite with VITE_, and Expo with EXPO_PUBLIC_, where anyone who installs the app can read the value. A secret in one of those is public the moment you deploy.
- Instead: put only publishable keys there. Supabase's publishable key and Stripe's pk_ key are made for it; Supabase's secret key bypasses Row Level Security and Stripe's sk_ key can do anything on your account, so both stay on the server.
- Call OpenAI, Anthropic and your email service from your server, never from the browser or the app.
- When a cloud build needs a key, set it in the build service too. A build that doesn't read your local .env ships the variable empty, often without an error.
Mistake: printing a key to check it
"Show me the key so I can see it's set" puts the key in the chat, and the chat is saved. The same happens without anyone asking: printenv, cat .env and the commands that list or diff a tool's config print values as a side effect.
- Instead: check a key by calling the provider with it by name and reading the status code. 200 means it works, 401 or 403 means it doesn't.
- Send the key to the kind of endpoint it is for. A user's session token sent to an admin API fails whether it is live or dead, so that 401 proves nothing.
- To see which variables exist, list names only: cut -d= -f1 .env.local.
Mistake: a rule written only in words
Claude Code's own docs draw the line: its permission rules are enforced by Claude Code, while instructions in CLAUDE.md only shape what Claude tries to do. A sentence asking the agent not to read .env files is a request. A deny rule is a wall.
- Instead: add deny rules for reading .env files, and ask rules for the commands that print config.
- Run a secret scan on every commit. gitleaks installs as a pre-commit hook with a five-line config file.
- Leave GitHub's push protection on. For your own account it is on by default and stops recognised keys from reaching a public repository.
- Know the gap: Apiiro ran gitleaks and TruffleHog over the 1,122 files, and 215 were flagged by neither, because a password typed into a command has no key format to match.
Mistake: one key for everything
Your agent's key lives in the most leak-prone place you have: the files and transcripts on your own computer. If it is also your production key, a leak there is a leak in production.
- Instead: give each consumer its own key: one for your agent on your computer, one for production, one for CI.
- Give each key only the permissions its job needs. Stripe's restricted keys (rk_) let you choose them, and Stripe lets you tag one as a key for an AI agent.
Mistake: deleting the file and calling it fixed
Removing a key from your code doesn't remove it from git history, forks or clones. Apiiro counted 734 secrets that now live only in a file's history.
- Rotate the key at the provider: create a new one and revoke the old one.
- Update every place that holds a copy the same day: your host, CI, the mobile build service and your own .env.
- Call the provider with the old key and expect a 401 or 403. Creating a new key doesn't always revoke the old one.
- Then clean up the files and, if you need to, the history.
Keep a SECRETS.md your agent reads first
Put all of the above in one file in your project: where each secret lives, the rules, which keys on your stack are public, the commands that print keys, and a Learned section the agent adds to each time something surprises you. Point your CLAUDE.md or AGENTS.md at it, and the agent reads it before any work that touches a key. This is how the IndieHacked team keeps its own projects' rules, and the Secrets setup writes a starting file for your stack.
Set it up with your coding agent
A prompt for Claude Code, Codex, Cursor or the agent you use. Fill in your setup at the end and it walks you through the work, one step at a time. Free with an account.
Questions founders ask
Is it safe to let an AI coding agent use my API keys?
Yes, when it reads them from where they live instead of seeing their values. Keep keys in a git-ignored .env file and your host's environment variables, have the agent refer to them by name, and block it from reading .env files.
Does .gitignore protect a file I already committed?
No. Git keeps tracking a file it already knows about. Untrack it with git rm --cached and the path, commit, and rotate any key it ever held, because the old commits still have it.
Can a Supabase or Firebase key sit in my frontend?
Supabase's publishable key and Firebase's web API key can, because Row Level Security and Security Rules decide what they reach. Supabase's secret key and a Firebase service account key must stay on your server.
I pushed a key to GitHub. What do I do first?
Rotate it at the provider before anything else, then update every place that uses it, check the old key now fails, and clean up the repository. Deleting the commit first leaves the key live in every fork and clone.
Where to go next
- Claude Code's settings files expose secrets on GitHub
Apiiro's count of the keys agents saved into committed files.
- The agent skills to install before you build a web app
Supabase's and Stripe's own skills cover their keys too.
More guides
All guidesBuilding with AI9 min read
The most common mistakes AI makes on the web
AI search8 min read
How to claim your business in ChatGPT and add products
A new guide every few days.
Weekly Unpack brings each one to your inbox. One email a week with founder stories, the playbooks behind them and the businesses we unpack in our interviews.
Plusand the Builder Kit come with it
Subscribing makes your account Plus and opens your Builder Kit: what your product needs next, written for your coding agent to do.