Guide · Env files

Keep API keys out of your .env files

An env file that holds real keys gets copied, synced, pasted and read by every tool in the folder. Keep the names in the file and the values in your Mac's iCloud Keychain instead.

In shortRun lk import .env.local --rewrite. Each secret moves into iCloud Keychain and the file keeps a latchkey:// reference in its place. Then start the project with lk run --env-file .env.local -- npm run dev, and Latchkey fills the values in for that command only.

Where plain values leak

A .env file is ignored by Git, but nothing else ignores it. Backups and cloud-synced folders copy it, screen shares show it, it gets pasted into chat to "just get the build working", and any program you run in the project, a coding agent included, can open it. Rotating a key means finding every copy.

Move the values into Keychain

  1. Install Latchkey and its command-line tool

    Download it free or install it with Homebrew, whose cask adds latchkey and its short name lk. With the disk image, open it once and choose Latchkey › Install Command Line Tool…. With the Mac App Store version, see the command-line tool.

  2. Preview the import

    Terminal
    lk import .env.local --dry-run

    The dry run prints item IDs and counts, never values. Each line is classed first: secrets are saved masked, settings such as PORT stay in the file, public values such as NEXT_PUBLIC_* are saved as plain values, and anything unclear is treated as a secret.

  3. Import and rewrite the file

    Terminal
    lk import .env.local --rewrite

    One Touch ID approval saves everything. Comments, order and the settings stay as they were:

    .env.local, after
    STRIPE_KEY=latchkey://shop/development/STRIPE_KEY
    DATABASE_URL=latchkey://shop/development/DATABASE_URL
    PORT=3000
  4. Run the project the same way as before

    Terminal
    lk run --env-file .env.local -- npm run dev

    Your app still reads process.env.STRIPE_KEY; the value is filled in when the command starts and never written back to the file.

  5. Catch the next plain value before it's committed

    Terminal
    lk scan --install-hook

    The pre-commit hook refuses a commit that adds an env file line holding a stored value or something that looks secret. It names the file, line and item, never the value.

Many projects at once

Terminal
lk import --scan ~/Projects --share-identical --dry-run

--scan finds every env file under a folder and skips examples, backups and dependency folders. With --share-identical, a key that has the same value in several projects becomes one shared item, so rotating it is one change.

What this doesn't do

A reference in a committed file gives away a name, not a value. But once a command has a value, it's in that command's memory, and Latchkey can't control what the command does with it. Masking hides exact copies in its output, not altered ones. The security page covers where protection ends.

Related