Get Started
Protect your app and website

Mission 6 of 10

Stop your API keys leaking, and know what to do if one does.

A key in a git commit is found by bots within minutes. Set up the env-file pattern, scanning on every commit (a net, not a lock), and a printed drill for the day a key gets out anyway.

60 minBeginnerFreeStep 1 free

What you'll do

Keep secrets out of code

Use the env-file split, keep server-only keys out of the browser bundle, and give each environment its own key.

Scan every commit

Add a pre-commit hook and a GitHub Actions scan that catch a secret before it can merge.

Rotate and rehearse a leak

Follow a rotation checklist and a printed first-ten-minutes drill for when a key gets out.

  • A real pre-commit config your whole team shares
  • Free tools only: Gitleaks, pre-commit and GitHub Actions
Before you start
  • A project with a git repository. Any language
  • Python with uv (for uvx, which runs pre-commit and Gitleaks with no install) or pre-commit installed another way
  • A GitHub repo if you want the CI scan (optional but recommended)
  • Having run find leaked secrets in code helps; this mission is the prevention side of it
Your progress0/5
01/05The env-file pattern: template committed, real values not0 done
STEP 01 of 05~10 min

The env-file pattern: template committed, real values not

The foundation. Get this split right and most leaks become impossible.

Best practicesecrets live in environment variables, read at runtime, never in source code. The standard way to manage them in development is a .env file, and the trick that makes it safe is a two-file split.

The two files

.env.example holds the *names* of every variable with empty values, and you commit it, so anyone cloning the repo knows what they need to provide.

.env holds the real values on each machine, and you never commit it.

  1. Set up the pair: a committed template with names only, and a real file that stays on this machine.

    The two-file split
    # .env.example  (COMMIT this: names only, no values. The template for a new machine.)
    DATABASE_URL=
    STRIPE_SECRET_KEY=
    RESEND_API_KEY=
    ANTHROPIC_API_KEY=
    SESSION_SECRET=
    
    # .env  (NEVER commit this: the real values live here, on this machine only.)
  2. Now make the mistake impossible with .gitignore. Even if you git add . out of habit, the real file won't be staged.

    .gitignore
    # .gitignore: make it impossible to commit the real one by accident.
    .env
    .env.*
    !.env.example
    *.pem
    *.key
    secrets/

Why this works

The leak happens when a real value ends up somewhere it's copied: a commit, a Docker layer, a log, a screenshot shared in Slack.

The two-file split means the file with real values is structurally excluded from the one thing that copies it most (git), and the committed file has no values to leak. .env.example also doubles as documentation: it's the single list of what the app needs to run.

Loading the file, and what happens in production

Load the file in code with your framework's support (process.env in Node with a loader like dotenv or built-in in Next.js; os.environ in Python with python-dotenv or uv's env handling).

In production you don't use a .env file at all: the platform (Vercel, Fly, your VPS's systemd unit, a secrets manager) injects the variables, and the file never exists on the server.

Warning
Check .env isn't already tracked from before you added the ignore rule: git ls-files | grep env. If git check-ignore .env prints nothing or git ls-files lists it, it's committed. git rm --cached .env, commit, and treat every value in it as leaked (the drill in step 6).
Checkpoint
git check-ignore -v .env names your .gitignore, .env.example is committed with empty values, and git ls-files | grep -i env shows only .env.example.
If it breaksMy framework needs the vars at build time, not runtime + 1 more
My framework needs the vars at build time, not runtime
Build-time vars that end up in the server bundle are fine; build-time vars that end up in the *client* bundle are the trap (next step). For CI builds, put the values in the CI's secret store (GitHub Actions secrets), not in a committed file.
I have lots of env vars and the example drifts out of date
Generate it: grep -oE '^[A-Z_]+=' .env | sed 's/=.*/=/' > .env.example (or a script that reads which vars your code references). Regenerate it when you add a key.
Still stuck? Ask the community
Next mission · Protect your app and websitePrompt injection: make your own agent leak data in 20 minutes, then make it stop
→