Secure API Key Storage

Ma Mahalakshmi V Updated 19 Sep 2026
7 min read ·Lesson 155 of 224

Avoiding API keys in source code and repositories

Lesson 1 of this unit established why an API key must be treated as a high-value credential. This lesson focuses on the single most common way keys actually leak in practice: they end up committed to a source code repository. This is not a rare mistake — automated scanners run continuously against public GitHub repositories specifically looking for strings that match API key formats, and a key pushed to a public repo can be found and abused within minutes.

Why source code is a bad home for secrets

A hardcoded key looks convenient in the moment:

# DO NOT DO THIS
from openai import OpenAI

client = OpenAI(api_key="sk-proj-abc123examplekeydonotusethisformat")

This line works, and that is exactly the problem — it works well enough that it is tempting to leave in place "temporarily." But source code is designed to be shared, copied, and preserved:

  • It is committed to version control, which keeps a permanent record of every version of every file, including ones you later "fix."
  • It is often pushed to a remote host (GitHub, GitLab, Bitbucket), where it may be cloned, forked, mirrored, or indexed by search engines and bots.
  • It is copied into new environments constantly — every git clone, every CI checkout, every teammate's laptop.

None of these are things you want a secret to inherit. A .env file or a secret manager entry (Lesson 3) is scoped to one machine or one deployment; a line in main.py is scoped to everywhere that repository ever goes.

The .gitignore pattern

The first line of defense is keeping secret-bearing files out of version control entirely. The standard pattern is:

  1. Store secrets in a file that is not tracked by git — conventionally .env.
  2. List that file in .gitignore so git refuses to track it even if someone runs git add ..
  3. Commit a .env.example (or .env.sample) file that documents which variables are needed, with placeholder values, so teammates know what to create locally without ever seeing a real secret.
# .gitignore
.env
.env.*
!.env.example
# .env.example  (safe to commit)
OPENAI_API_KEY=your-api-key-here
OPENAI_PROJECT_ID=your-project-id-here
# .env  (never committed — this is the real one, local only)
OPENAI_API_KEY=sk-proj-REALVALUEHERE

The !.env.example line in .gitignore is a negation pattern: it says "even though .env.* is ignored, make an exception for .env.example." This lets you version-control the shape of your configuration without ever version-controlling the values.

Your Python code then loads the real file at runtime (see Lesson 3 for the full python-dotenv pattern), while git never sees its contents.

Detecting secrets before they are committed

.gitignore only protects files that are never staged in the first place. It does nothing if a key is pasted directly into a tracked .py, .json, or .yaml file. For that, use a pre-commit secret scanner — a tool that inspects staged changes and blocks the commit if something that looks like a credential is found.

A minimal version of the idea, implemented as a standalone check you could wire into a pre-commit hook or CI step, looks like this:

import re

# Patterns that plausibly match common secret formats.
SECRET_PATTERNS = [
    re.compile(r"sk-[A-Za-z0-9]{20,}"),          # OpenAI-style API keys
    re.compile(r"AKIA[0-9A-Z]{16}"),              # AWS access key IDs
    re.compile(r"-----BEGIN (RSA|EC|DSA)? ?PRIVATE KEY-----"),
]


def find_suspected_secrets(file_content: str) -> list[str]:
    """Return any substrings in file_content that look like secrets."""
    findings = []
    for pattern in SECRET_PATTERNS:
        findings.extend(match.group(0) for match in pattern.finditer(file_content))
    return findings


def check_files_for_secrets(files: dict[str, str]) -> dict[str, list[str]]:
    """
    files: mapping of filename -> file content (as would be staged for commit).
    Returns a mapping of filename -> list of suspected secrets found, for
    files where at least one match was found.
    """
    problems = {}
    for filename, content in files.items():
        findings = find_suspected_secrets(content)
        if findings:
            problems[filename] = findings
    return problems

This is intentionally simplified — production tools like detect-secrets, gitleaks, or truffleHog use a much larger and better-maintained set of patterns, entropy analysis (flagging high-randomness strings even without a known prefix), and historical scanning across the whole git log. But the shape of the check is the same: pattern-match staged content against known secret formats and refuse the commit if something matches.

A dependency-injected test, with no real files or real secrets touched:

def test_detects_openai_style_key():
    files = {
        "config.py": 'API_KEY = "sk-proj-abcdefghijklmnopqrstuvwxyz123456"',
        "readme.md": "This project uses the OpenAI SDK.",
    }
    problems = check_files_for_secrets(files)
    assert "config.py" in problems
    assert "readme.md" not in problems
    print("PASS: secret scanner flags the file containing a key-like string")


def test_clean_files_pass():
    files = {"app.py": "import os\napi_key = os.environ['OPENAI_API_KEY']"}
    problems = check_files_for_secrets(files)
    assert problems == {}
    print("PASS: scanner does not flag code that reads from the environment")


test_detects_openai_style_key()
test_clean_files_pass()

The second test matters as much as the first: a scanner that also flags legitimate, secret-free code (reading from os.environ) is not useful, because developers will learn to ignore or bypass it.

If a key is already committed: revoke first, scrub second

This is the point most teams get wrong. When a key is discovered in a repository's history, the instinct is to remove it from the code and rewrite git history to erase it. History rewriting is useful, but it is not the fix — it is cleanup.

The only action that actually stops the exposure is revoking the key in the OpenAI dashboard and issuing a new one. Once a secret has been pushed to a remote repository — especially a public one, or even a private one with more than a couple of collaborators — you must assume it has been seen, cloned, cached, or indexed somewhere outside your control. Rewriting history on your own copy of the repository does not reach any of those other copies.

The practical order of operations is:

  1. Revoke the exposed key immediately in the OpenAI dashboard and generate a replacement.
  2. Update every environment (local .env files, CI secrets, production secret manager) with the new key.
  3. Remove the secret from the current code so the mistake isn't repeated in the next commit.
  4. Optionally, scrub git history with a tool like git filter-repo (the modern replacement for the older BFG Repo-Cleaner / git filter-branch approaches) if you want the string gone from the repository's history for hygiene or compliance reasons.

Note on history scrubbing: Rewriting history changes every commit hash after the rewritten commit. Anyone with an existing clone will need to re-clone or carefully re-base their work, and any open pull requests referencing the old commits will likely need to be recreated. Coordinate with your team before doing this, and never treat it as a substitute for revocation — a scrubbed-but-not-revoked key is still a valid, working credential.

Common Mistakes

  • Believing that deleting a file in a later commit removes the secret. Git preserves every prior version in its history by default; the key is still retrievable with git log -p or by checking out the earlier commit, even after a "fix" commit removes it from the latest version.
  • Committing to a private repository and assuming that makes it safe. Private repositories are still cloned by teammates, mirrored by CI systems, and sometimes accidentally made public later. Treat every repository, private or not, as a place secrets do not belong.
  • Scrubbing history without rotating the key. As covered above, this leaves a fully functional credential in place while giving a false sense of resolution.

Best Practices

  • Add secret-bearing filenames to .gitignore before creating the files, not after, so there is never a window where the real file could be accidentally staged.
  • Run an automated secret scanner as a pre-commit hook and in CI, so a leak is caught before it reaches the remote repository, not after.
  • Commit an .env.example file so new contributors know what configuration is required without ever needing to see or copy a real secret.
  • Treat any leaked key as compromised the moment it is pushed, and revoke it immediately rather than waiting to assess whether anyone actually saw it.

0 Comments

Reviewed before they appear

No comments yet.

OpenAI SDK
Introduction to the OpenAI SDK Setting Up Python Creating an API Key Your First Call — client.responses.create() and response.output_text Understanding Billing, Credits, and What a Request Costs Why Responses Replaced Chat Completions Anatomy of a Request: model, input, and instructions Anatomy of a Response: The Typed output Array, Not Just Text Roles: User, Assistant, and Developer/System Choosing a Model, and Reading the Models Page Instead of Memorizing Names Instructions vs. Input Writing Prompts That Get Consistent Results Few-Shot Examples Reasoning Models and the reasoning Parameter Debugging a Prompt That Misbehaves Why Streaming Matters for User Experience stream=True and Iterating Over Events Handling the Event Types You Actually Care About Background Mode for Long-Running Jobs Project — Add Live Streaming to Your Chatbot The Problem With Parsing Free Text JSON Schema and Strict Mode Pydantic Models With the SDK's Parse Helpers Handling Refusals and Validation Failures Project — A Resume-to-JSON Extractor Working With input_image input_file, PDFs, and the Files API Image Generation Speech-to-Text and Text-to-Speech Project: A PDF Question-Answering Script What Function Calling Is Defining a Tool Schema The Full Loop Multiple Tools Errors, Timeouts, and Untrusted Arguments Project: A Weather Assistant Web Search File Search and Vector Stores Code Interpreter Remote MCP Servers and Connectors Project: A Research Assistant What an Embedding Is, Without the Maths Generating and Storing Embeddings Similarity Search From Scratch Hosted Vector Stores vs. Rolling Your Own A Small RAG App Over a Folder of Notes Agents vs. a Single API Call — When You Need One pip install openai Giving Agents Tools Handoffs and Multi-Agent Triage Guardrails and Approvals Tracing and Observing What Your Agent Did A Multi-Agent Support Desk Error Codes and What Each One Means Retries, Timeouts, and Backoff Rate Limits and Spend Limits Prompt Caching and Cost Optimisation The Batch API for Bulk Work Async Clients and Concurrency Moderation and Safety Best Practices Designing the App Backend With FastAPI Streaming to a Simple Frontend Deploying and a Cost/Safety Checklist Why Web Search Is Useful for Current Information Using the Web Search Tool with the Responses API Configuring Search Behavior for Application Use Cases Understanding Citations and Source Attribution Where to Go Next Building a Research Assistant with Web Search Combining Web Search with Structured Outputs Handling Conflicting or Low-Quality Web Sources Reducing Unsupported Claims with Grounded Generation Testing Freshness-Sensitive AI Answers Production Considerations for Web-Grounded Applications Understanding File Search and Retrieval-Augmented Generation Creating and Organizing Vector Stores Uploading Documents for Retrieval Connecting Vector Stores to Responses API Requests Designing Document Metadata and Filtering Strategies Building a PDF Question-Answering Application Improving Retrieval Quality With Better Document Preparation Handling Missing Evidence and Retrieval Failures Combining File Search With Web Search Building a Production Knowledge-Base Assistant What the Code Interpreter Tool Is Designed For Running Python-Based Analysis Through the OpenAI SDK Uploading Datasets for Analysis Analyzing CSV and Spreadsheet Data Generating Charts and Data Summaries Handling Generated Files and Downloadable Artifacts Building a Data-Analysis Assistant Combining Code Execution with Structured Outputs Validating Generated Calculations and Results Security and Sandbox Considerations for Code Execution Understanding Multimodal Input with the OpenAI SDK Sending Images to a Model Image Analysis from URLs and Uploaded Files Extracting Text and Information from Screenshots Building an Image-Question-Answering Application Combining Image Input with Structured Output Analyzing Multiple Images in One Request Handling Image Quality and Input Limitations Designing Multimodal Prompts for Reliable Results Building a Practical Vision-Powered Python Application Understanding Speech-to-Text and Text-to-Speech Workflows Transcribing Audio with the OpenAI SDK Working with Uploaded Audio Files Handling Timestamps and Transcription Metadata Building a Meeting Transcription Workflow Generating Spoken Responses from Text Handling Long Audio and Processing Failures Combining Audio with Text and Tool Calling Building an End-to-End Python Voice Application What Embeddings Are and When to Use Them Generating Embeddings With the OpenAI API Preparing Text for Embedding Comparing Vectors With Cosine Similarity Building a Simple Semantic Search Engine in Python Storing Embeddings in a Database Metadata Filtering for Semantic Search Chunking Strategies for Better Retrieval Evaluating Semantic Search Quality Building a Document Similarity Application When Batch Processing Makes Sense Designing Large-Volume AI Processing Pipelines Using Asynchronous Python with the OpenAI SDK Running Concurrent Requests Safely Controlling Concurrency and Avoiding Rate Limits Tracking Batch Job Progress Handling Partial Failures in Bulk Workloads Retrying Failed Items Without Duplicating Successful Work Designing Resumable AI Processing Jobs Building a Production Batch-Processing Pipeline Batch Processing Makes Sense Large-Scale AI Processing Pipelines Async Python with OpenAI SDK Safe Concurrent Requests Concurrency & Rate Limits Batch Progress Tracking Partial Failure Handling Safe Retry Handling Resumable AI Jobs Production Batch Pipeline System–User Data Separation Reusable App Instructions Prompt Templates & Variables Extraction & Classification Prompts Summarization & Transformation Prompts Explicit Output Requirements Prompt Version Management Prompt Testing & Evaluation Reusable Python Prompt Library API Key Security Secure API Key Storage Secure Secret Management Prompt Injection Prevention Trusted vs. Untrusted Content Tool Argument Validation Sensitive Data Handling Secure Logging AI Action Authorization Production AI Security Checklist Why AI Applications Need Evaluation Beyond Unit Tests Unit Testing OpenAI SDK Integration Code Mocking API Responses in Python Tests Testing Structured Outputs Against Schemas Testing Tool-Calling Workflows Building a Small Evaluation Dataset Measuring Accuracy, Consistency, and Failure Rates Regression Testing Prompts and Model Changes Human Evaluation Versus Automated Evaluation Creating a Repeatable Evaluation Pipeline AI Request Monitoring Token Cost Management Usage Metrics Design Reducing Model Calls Prompt & Context Optimization Model Selection & Optimization AI Caching Strategies Interactive Latency Optimization Usage Dashboards & Budget Alerts Performance & Cost Checklist Every API Call Starts Fresh Fixing API Statelessness Server-Side Conversation Memory Limits of Response Chaining What We're Building Conversation Memory Challenges Preparing an OpenAI SDK Application for Deployment Environment-Specific Configuration for Development and Production Deploying a Python AI Service with Docker Container Health Checks and Startup Configuration Managing Secrets in Cloud Deployments Background Workers for Long-Running AI Tasks Queues and Asynchronous Job Architectures Scaling AI Workloads Horizontally Monitoring Production Incidents and Failures Production Deployment Checklist for OpenAI SDK Applications Reusable OpenAI Service Classes AI Client Dependency Injection Typed AI Responses Python Configuration Management AI Request Decorators Centralized AI Error Handling Clean SDK Abstractions Reusable OpenAI Utilities Internal AI Python Libraries SDK Integration Maintenance Production AI Chatbot Document Q&A System Web Research Assistant Customer Support Agent AI Data Analysis Assistant Image Analysis App Meeting Transcription & Summary Semantic Document Search Multi-Tool AI Agent Production OpenAI SDK App Why "It Looked Fine When I Tested It" Isn't Enough Timing Note Status Note Pre-Decision Status Note Current Availability Note
Ask about this post
AI Ask about this post

Ask questions about Secure API Key Storage and get answers drawn from it.

Signed-in readers only.