LifeRemix.aiMake a little more possible.Join the good stuff ↗
Daily LifehackMy Agentic LifeThe Weekly Remix
remix with ai:Work & Business

The Code Sweep: Find and Fix Leaked API Keys in Your AI Prototype

AI coding assistants are brilliant at writing apps, but they often hardcode your secret passwords and API keys directly into the files. Here is how to scan your local prototype, securely rotate exposed keys, and store them safely.

A man checking his app code at a home workbench in the evening.
AI-generated illustrative photograph.

You poured a flat white, opened up an AI coding assistant, and three hours later you have built a fully functioning web app. Let’s imagine it is a shared recipe index for your flat, or a booking system for your weekend run club. It feels like absolute magic.

But before you proudly drop that link into the group chat, there is one tiny, essential lifehack you need to perform to keep your accounts safe. As the New Zealand Office of the Privacy Commissioner notes, generative AI models can generate computer code, but you must take care with how confidential information is handled. AI tools optimise for speed, not durability. To get your app running in record time, they have a naughty habit of pasting your secret API keys, database connection strings, and passwords directly into the source files.

If you are using version control (like Git) to save your work, that original hardcoded secret is permanently etched into your commit history. If you publish this code, anyone can sift through your past saves and find it.

A secret sweep will not magically make a weekend prototype ready for a commercial production launch—there are many more security layers required for that—but it will prevent you from accidentally broadcasting your credit-card-linked API keys to the entire internet. Here is how to find the leaks, rotate the keys, and properly secure them.

The Finish Line

A local codebase scanned for leaked secrets, any exposed credentials officially revoked and replaced, and a fresh environment file correctly configured to keep your new keys safe. Plus, a clean re-scan to prove it worked.

  • Time: 25 minutes (10 minutes setup, 15 minutes scanning and rotating keys).
  • Cost: Free.
  • Prerequisites: A local folder containing your AI-generated code (initialised as a Git repository), and a terminal window. Mac users will use Homebrew; Linux or Windows users can use Docker or native installation scripts.

1. Install the digital sniffer dog

To check our digital footprints, we will use an open-source scanner called TruffleHog. It does not just look at your current code; it scans your whole commit history for patterns that match over 800 real credential types and actively checks if they are live.

Open your terminal. If you are on a Mac, you can install it easily using Homebrew by typing:

brew install trufflehog

Note: If you are on Linux or Windows, ensure the Docker engine is running and refer to the official documentation for your specific command.

2. Sweep your project directory

Once installed, you need to run the scanner from the parent directory (the folder sitting just outside your actual project folder). In your terminal, navigate to that parent folder and run the following command, replacing your_project_folder with your actual app's folder name:

trufflehog git file://your_project_folder --results=verified,unknown

TruffleHog clones a temporary version of your repository and scans the entire history. If it finds a live key that the AI hardcoded three days ago, it will bark. Here is an illustrative example of what a failed check (a found secret) looks like, based on the official documentation:

🐷🔑🐷 TruffleHog. Unearth your secrets. 🐷🔑🐷
Found verified result 🐷🔑
Detector Type: AWS
Decoder Type: PLAIN
Raw result: AKIAYVP4CIPPERUVIFXG
Line: 4
Commit: fbc14303ffbf8fb1c2c1914e8dda7d0121633aca
File: keys

3. Revoke and rotate exposed keys

If the scanner finds a key in your history, do not just delete the text from your file and save it. According to GitHub's security documentation, your first and most effective step is simply to revoke the compromised password or token and generate a fresh one.

Assume the exposed key is compromised. Go directly to your provider's dashboard (for example, OpenAI, AWS, or your database provider), find the API keys section, and click "Revoke" or "Delete" on the exposed key. Generate a brand new one to replace it.

GitHub notes that once a secret is revoked, it can no longer be used for access, which is often sufficient to solve the immediate security problem without undertaking the highly risky process of rewriting your entire Git repository history.

4. Move the new keys to a safe house

Now that you have a fresh, secure key, you must hide it from your version control. According to the OpenAI Cookbook and standard development practices, you should store these in a local environment file rather than hardcoding them.

In the root folder of your project, create a plain text file named exactly .env (the dot at the start is deliberate). Open it in your code editor and paste your new key like this:

OPENAI_API_KEY=your_new_api_key_here

Crucial Step: You must ensure Git ignores this new file. Open your .gitignore file (or create one if it doesn't exist) and add a single line to it:

.env

By adding .env to your .gitignore, you instruct your version control to never track, save, or upload your secret keys.

5. The final verification scan

To prove your setup worked, navigate back to the parent directory in your terminal and run the exact same TruffleHog scan again:

trufflehog git file://your_project_folder --results=verified,unknown

According to the official TruffleHog documentation, if your repository is clean and your new keys are safely hidden out of sight, you will see a single welcome message before the program immediately exits:

🐷🔑🐷 TruffleHog. Unearth your secrets. 🐷🔑🐷

If it simply prints that line and exits, you have successfully crossed the finish line. Your local AI project is now swept, your compromised keys are revoked, and your active credentials are safely tucked away in the shadows where they belong.


AI disclosure: AI systems assisted with research and writing. Gemini independently peer-reviewed and approved this article before publication. Images, where present, are illustrative AI-generated photographs.