Skip to content
FlexibleToolsAI
Security Guide7 min read Updated November 2, 2026

How to Find and Remove Hardcoded API Keys in Code

Leaking an AWS key, Stripe secret, or OpenAI token in a public GitHub repository can lead to thousands of dollars in fraudulent usage or total account compromise within minutes. Automated credential crawlers monitor public commits 24/7. Here is how to audit your code, detect hardcoded secrets, and purge them safely.

Quick Takeaway

To find exposed API keys, run an in-browser scanner or CLI tool that combines provider prefix patterns (like AKIA, sk-proj-, sk_live_, ghp_) with Shannon entropy analysis to catch random strings assigned to variable names like api_key or secret. If a leaked key is discovered: 1) immediately revoke and rotate it in the provider's developer console, 2) check audit logs for unauthorized calls, 3) move the secret into environment variables, and 4) rewrite git history before pushing.

Instant Online Tool

Convert Your Files Right Now

No sign-up required, zero file uploads. 100% free and private in your browser.

Launch API Key Finder

1. The Anatomy of Common API Keys

Most major cloud and SaaS providers embed identifiable prefixes and checksums into their credentials: AWS Access Keys begin with AKIA or ASIA (20 characters), Stripe live keys begin with sk_live_ or rk_live_, GitHub personal access tokens use ghp_ or github_pat_, and OpenAI uses sk-proj- or sk-.

However, generic tokens (such as database connection strings, JWT signing keys, and private webhook secrets) don't have standard prefixes. Detecting them requires Shannon entropy calculation — measuring the mathematical randomness of character distributions to differentiate true cryptographic keys from regular variable names.

2. Scan Your Codebase Locally Before Committing

Always audit code and configuration files client-side rather than uploading them to untrusted web forms. Use our API Key Finder to paste code, drag and drop files, or inspect a full repository ZIP archive. The entire scan runs in your browser's local memory with zero server uploads.

Check for high-risk files that developers frequently commit by accident: .env, .env.local, config.json, settings.py, docker-compose.yml, and serverless.yml.

3. What to Do Immediately When a Key Leaks

First, assume the key has been compromised. Automated botnets continuously monitor GitHub commits, public pastebins, and package registries. Deleting the commit or turning a repository from public to private does not un-compromise the credential.

Go directly to the provider's console (AWS IAM, Stripe Dashboard, OpenAI Platform) and revoke the exposed key. Generate a replacement, inject it through environment variables or a secrets manager (such as AWS Secrets Manager, HashiCorp Vault, or Doppler), and inspect recent access logs for suspicious activity.

4. Purging Secrets from Git History

Simply making a new commit that deletes the API key leaves the secret in your repository's commit history. Anyone who clones the repo can retrieve it with git log.

To permanently scrub a credential from all commits, use git-filter-repo: `git filter-repo --replace-text expressions.txt` or BFG Repo-Cleaner: `bfg --replace-text passwords.txt`. After rewriting history, force push with `git push origin --force --all` and revoke the original key.

Try Free Web Tools Mentioned in This Guide

Frequently Asked Questions

Why isn't deleting the key in a new commit enough?↓

Git preserves every version of every file forever in its history tree. Anyone viewing the commit log can easily inspect older commits and read the hardcoded credential.

Can I use .gitignore to prevent leaks?↓

Yes. Adding .env, .env.*, *.pem, *.key, and credentials.json to your .gitignore is the primary defense. Ensure these files are ignored before making your initial repository commit.

Is it safe to paste code into an online API key scanner?↓

Only if the scanner executes 100% client-side in your browser, like our API Key Finder. Never upload source code to web tools that transmit data to remote servers or third-party cloud storage.

Related Guides