---
title: "Block API keys before they reach GitHub"
description: "Stop API keys, tokens and passwords from being committed: a one-line pre-commit hook and a copy-paste GitHub Action, both free, using npx keysquid scan."
date: 2026-11-30
updated: 2026-11-30
author: "Keysquid team"
category: "Guides"
tags: [secret-scanning, github, pre-commit, api-keys, ci]
primary_keyword: "block api keys before github"
draft: false
faq:
  - q: "Do I need a Keysquid account to use keysquid scan?"
    a: "No. The scanner runs on your computer or in your CI and needs no account, no sign-in and no network access to Keysquid. It only reads your files."
  - q: "Does the scanner send my code or keys anywhere?"
    a: "No. It reads files locally and prints file, line and a shortened preview (the first and last characters) of anything that looks like a key. The full value is never printed or sent."
  - q: "How do I allow a line that isn't really a secret?"
    a: "Add the comment keysquid:ignore to that line, for example in a test fixture. The scanner skips it from then on."
  - q: "I already pushed a key. Is deleting the commit enough?"
    a: "No. Treat the key as leaked: replace it at the provider first, then clean up the history. Bots scan public GitHub within minutes, and forks and caches keep old commits."
  - q: "How do I skip the hook once?"
    a: "Run git commit --no-verify. Use it rarely: the hook exists for the one time you didn't notice the .env in your staged files."
sources:
  - https://docs.github.com/en/code-security/secret-scanning/introduction/about-secret-scanning
  - https://git-scm.com/docs/githooks#_pre_commit
  - https://www.npmjs.com/package/keysquid
---

The cheapest leaked key is the one that never leaves your laptop. **Run `npx keysquid scan install-hook` once in your repository, and every commit that contains an API key, token or password is stopped before it's made.** Add the GitHub Action below as a second net, so a pull request with a key in it fails before anyone merges it. Both are free, and neither needs a Keysquid account.

## Why a hook and a CI check, not one or the other

A pre-commit hook is fast and personal. It catches the key at the moment it would become part of history, which is the cheapest moment to catch it: nothing has been pushed, nothing needs rotating, and you just remove the line. Its weakness is that it lives in each developer's clone. A new teammate, a fresh laptop or an AI coding agent working in a container won't have it until someone runs the install command again.

A CI check is the opposite. It runs for everyone, on every pull request, without anyone remembering to set it up, but it runs after the push. By the time it fails, the key is already on GitHub, even if only on a branch. On a private repository that's usually fine: the branch never gets merged and the key is changed. On a public repository, assume it's been seen.

Run both. The hook stops most mistakes before they exist; the CI check stops the rest from reaching `main`.

## What the scanner looks for

The scanner uses the same detection rules Keysquid uses when you import an app: the recognisable shapes of keys from OpenAI, Anthropic, Stripe, GitHub, AWS, Google, Slack, Resend, Twilio and many more, plus database URLs that contain a password and private key blocks. It looks at the structure of the text (a known prefix, the right length, the right characters), not just at variable names, so `OPENAI_API_KEY=` with an empty value is not a finding, and a real key pasted into a comment is.

It deliberately ignores folders that are never committed by hand (`node_modules`, `.git`, `dist`, `build`, virtual environments) and binary files such as images and fonts. Files larger than 2 MB are skipped too, since generated bundles are where most noise comes from.

No scanner is perfect. Some keys have no recognisable shape at all (a random 32-character password looks like any other string), and some test fixtures look exactly like real keys. That's why the output is a list to check rather than a verdict, and why `keysquid:ignore` exists.

## How this compares with GitHub's own protection

GitHub has secret scanning and push protection of its own, and if you have them, keep them on. They work on GitHub's side, after the push is attempted, and the free tier covers public repositories; private repositories need a paid plan for full coverage. A local hook catches the key earlier, before the commit exists, and works the same in private repositories, on any Git host and inside an AI agent's sandbox. The two complement each other: one protects your laptop, the other protects the server.

Open-source tools such as gitleaks and TruffleHog do a similar job and are well worth a look if you already run them. `keysquid scan` is aimed at teams who want the shortest possible setup, one command with nothing to configure, and who may later want the keys it finds moved somewhere they can be rotated.

## Step 1: check what's already there

From the root of your repository:

```sh
npx keysquid scan
```

It walks your files (skipping `node_modules`, `.git`, build output and binaries) and lists anything that looks like a key:

```text
src/lib/openai.ts:4:18  OpenAI API key  sk-p…Mn (58 chars)
.env.example:2:14  Database URL with password  post…id (57 chars)

2 possible secret(s). Move them into Keysquid and rotate any that were committed.
```

Values are shortened on purpose, so the output is safe to paste into an issue or a chat. The command exits with code 1 when it finds something, which is what makes it useful in hooks and CI.

If a finding is fine (a fake key in a test, an example with a placeholder), add `keysquid:ignore` as a comment on that line.

## Step 2: block commits on your machine

```sh
npx keysquid scan install-hook
```

This writes a small `pre-commit` hook into `.git/hooks`. From then on, `git commit` checks only the files you're committing (`keysquid scan --staged`), which takes well under a second. If something looks like a key, the commit stops and tells you where.

Git hooks live in each person's clone and aren't pushed, so every teammate runs the command once. That's why step 3 matters.

## Step 3: block pull requests in GitHub

Create `.github/workflows/secret-scan.yml`:

```yaml
name: secret-scan
on:
  pull_request:
  push:
    branches: [main]
permissions:
  contents: read
jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - run: npx --yes keysquid scan
```

Commit it, and every pull request gets a red cross when it contains something that looks like a key. In **Settings → Branches**, make the `secret-scan` check required so nobody can merge past it.

## If the scanner finds a real key

Don't just delete the line. A key that was ever committed should be treated as public:

1. **Replace it at the provider first.** Make a new key, put it where your app reads it, and redeploy.
2. **Then revoke the old one.** Only after the new key works everywhere.
3. **Clean up the history** if the repository is public, knowing that forks and caches may still have it.

Our guide on [what to do in the first 10 minutes after a leak](/blog/leaked-api-key-first-10-minutes) goes through this step by step.

## Keys in AI coding agents

AI coding agents read your files and run commands, which makes them very good at finding a key you forgot about, and occasionally at copying it somewhere it shouldn't be. The hook applies to their commits too, as long as they commit through Git in a clone where it's installed. For agents that run in a fresh container, add the CI check; it doesn't care who wrote the commit. Our guide on [keeping API keys away from Claude Code](/blog/keep-api-keys-away-from-claude-code) covers the other half: making sure the agent never has the key in the first place.

## Where Keysquid comes in

A scanner tells you a key is in the wrong place. Keysquid gives it the right place: your keys live encrypted with an agent you run yourself, your apps get them at deploy time, and keys at OpenAI, GitHub, Cloudflare, Resend and 30+ other providers are replaced on a schedule, so a key that slips out anyway is already on its way to being useless. See [how the agent and encryption work](/security) or [the docs](/docs) to set it up, and the [list of providers Keysquid rotates](/rotate).
