Free full scan for Mac. No account. Nothing leaves your device. Download free
Prycheck Download free
Security guide

API Keys in Plain Text, How to Find, Remove and Rotate

Plain text API keys can be read by any app running as you. Learn where to find them on your computer, how to remove them, and how to rotate them safely.

An API key in plain text can be read by any program running under your account, including AI agents and the tools they start. Search your config files, project folders and git history, then treat any exposed key as used: rotate it first, and clean up the copies after. Keep new keys in a password manager or secrets tool, with small scopes and spending limits.

A plain text file on your own disk is not the same as a public leak. Still, AI tools raise the stakes. An agent that can read project files can read your .env file too, and it may pass that text to a model. This guide covers how to find keys, how to replace them, and how to stop them coming back.

Where keys usually hide

Check these places first:

  • .env files in project folders
  • Shell profiles, such as ~/.zshrc, ~/.bashrc and ~/.bash_profile
  • Shell history, such as ~/.zsh_history
  • MCP and agent configs, especially env blocks
  • Cloud tool credentials, such as ~/.aws/credentials
  • Notes apps, text files on the Desktop and old chat exports
  • Code, test scripts and notebooks, including their saved outputs
  • Git history, even for files you later deleted

How to find keys on Mac and Linux

Search for common key shapes. These are a starting point, not a full list, and they will miss some formats. The patterns below match common prefixes from OpenAI, Anthropic, Google, GitHub and AWS.

  1. Open Terminal.
  2. Run this search on your projects folder, changing the path as needed: grep -rIlE 'sk-[A-Za-z0-9_-]{20,}|AIza[0-9A-Za-z_-]{30,}|ghp_[A-Za-z0-9]{30,}|AKIA[0-9A-Z]{16}' ~/projects
  3. Run the same search on single files, such as ~/.zshrc and ~/.zsh_history.
  4. Review the file names it returns. The -l flag lists only names, so keys do not appear on your screen or in a shared log.

How to find keys on Windows

  1. Open PowerShell.
  2. Run this search, changing the path as needed: Get-ChildItem $HOME\projects -Recurse -File -ErrorAction SilentlyContinue | Select-String -Pattern 'sk-[A-Za-z0-9_-]{20,}' -List | Select-Object Path
  3. Repeat it with other patterns from the Mac section.
  4. List variable names without their values: Get-ChildItem Env: | Where-Object Name -match 'KEY|TOKEN|SECRET' | Select-Object Name
  5. Check %APPDATA%\Claude\claude_desktop_config.json and other agent configs.

How to check git history

Deleting a file does not remove it from past commits. Use a scanner made for this job. Gitleaks is an open source tool that can scan a repository's history. See its help output for the command in your version.

To check one specific key, search for its first few characters: git log --all --oneline -S "first-part-of-key". Any commit it lists added or removed that text.

GitHub also offers secret scanning. It runs at no cost on public repositories, and push protection helps block known secret types before they land. For private repositories, this needs a paid plan. See GitHub's security features for what your plan includes.

If you want a quick look at your device as a whole, one way to check this is Prycheck. It is a local app that looks for keys in plain text on your device and sends nothing anywhere. It tells you where a key sits, but it cannot tell you whether anyone has used it.

How to rotate a key

The order matters. Rotate first, because removing a key from a file does nothing about copies that were already taken. GitHub's guide to removing sensitive data makes the same point: once a secret can no longer grant access, you may not need to rewrite history at all.

Choose your path based on what happened.

If the key was exposed

This covers a public repository, a shared log, a screenshot, or a chat with others.

  1. Revoke the key now in the provider's console, even if something stops working.
  2. Create a new key with the smallest scope that works.
  3. Update your apps with the new key.
  4. Check the provider's usage and logs for activity you do not recognize.
  5. Contact the provider if you see any, and check the billing page too.

OWASP's secrets management guidance also says to revoke an exposed secret right away, then rotate it.

If the key sat in a local file

If you found it on your own device with no sign of misuse, you can avoid downtime.

  1. Create a new key.
  2. Update every app and service that uses the key, and confirm they work.
  3. Revoke the old key.
  4. Check usage for the old key in the days before you revoked it.

Key pages vary by provider. Anthropic's are in the Claude Console at Settings > API keys, and it advises deleting a suspect key from there. OpenAI lets you rotate keys from its API Keys page. For other providers, look for API keys or credentials in the account settings.

How to remove the old copies

  1. Delete the key from each file you found. Remove the line, not just the value, if it is no longer used.
  2. Clear the line from your shell history, or delete the history file. On Mac, open ~/.zsh_history in an editor, remove the line and save.
  3. Add .env to your .gitignore file so it cannot be committed by accident.
  4. If a key is in git history and you must remove it, use git filter-repo version 2.47 or later, as GitHub describes. Expect commit hashes to change, and have collaborators rebase.
  5. Remember backups, sync folders and copies on other machines.

How to store keys from now on

No method is perfect, so pick the best fit.

  • Secrets manager or password manager: the best choice for most people. Many have a command line tool that loads a key when needed.
  • Mac Keychain: run security add-generic-password -a "$USER" -s openai-api-key -w. Keep -w as the last option, so it prompts you and the key stays out of your shell history. Read it with security find-generic-password -a "$USER" -s openai-api-key -w.
  • Environment variables: better than keys in code. OpenAI recommends this approach over hardcoding. OWASP notes that variables can end up in logs and dumps, so they are not a vault.
  • Editor prompts: VS Code input variables ask for a value once and store it securely.

Then limit the damage from any future leak:

  • Use a separate key per person, project and environment.
  • Set spending limits and rate limits.
  • Add expiry dates where the provider supports them.
  • Rotate on a schedule. Anthropic gives every 90 days as an example.
  • Add a pre-commit secret scan to your repositories.
  • Never put an API key in browser or mobile app code.

Frequently asked questions

Is a .env file safe?

It is safer than hardcoding a key, as long as it stays out of git. But it is still plain text. Any program or agent running as you can usually read it, so use small scopes and keep real secrets elsewhere when you can.

Do I need to rotate a key that was only on my own computer?

If nothing suggests exposure, the risk is lower, but rotation is cheap. Rotate it if an AI agent, a shared folder, a backup or a log may have touched the file.

Is deleting the key from my repository enough?

No. It stays in git history, and it may be in forks and clones. Revoke the key first. After that, a history rewrite may be optional.

Find out what AI can reach on your computer.

The full scan is free. It takes minutes, needs no account, and stays on your device.

Download free for Mac
Download free