Skip to main content

A local secret leak detection tool for developers and DevOps teams.

Project description

๐Ÿ” apikeyscanner

A local secret leak detection tool for developers and DevOps teams.

Detect leaked API keys, tokens, passwords, and secrets inside your files and projects โ€” before they reach production or get pushed to GitHub.


โœจ What It Does

apikeyscanner scans local files and directories for hardcoded secrets like:

  • OpenAI, Anthropic, HuggingFace API keys
  • AWS, Google Cloud, Azure credentials
  • GitHub, GitLab personal access tokens
  • Stripe, PayPal payment keys
  • Slack, Discord, Telegram bot tokens
  • Database connection strings (PostgreSQL, MySQL, MongoDB, Redis)
  • Hardcoded passwords, secrets, and tokens
  • JWT tokens and Bearer tokens
  • Private key blocks (RSA, EC, OpenSSH, PGP)
  • .env file sensitive values

๐Ÿ“ฆ Installation

# Install from PyPI
pip install apikeyscanner

# Clone the repository
git clone https://github.com/devxyasir/apikeyscanner.git
cd apikeyscanner

# Install in development mode
pip install -e .

# Or install with dev tools
pip install -e ".[dev]"

โšก Quick Start

CLI

# Scan the current project
apikeyscanner scan .

# Scan a specific file
apikeyscanner scan ./config.py

# Scan a directory
apikeyscanner scan ./src

# Only show HIGH severity findings
apikeyscanner scan . --severity HIGH

# Save a JSON report
apikeyscanner scan . --output reports/report.json

# Ignore specific directories
apikeyscanner scan . --ignore node_modules --ignore venv

# Print raw JSON output (great for CI/CD)
apikeyscanner scan . --json

Python Library

import apikeyscanner as aks

# Scan a file
result = aks.scan("./config.py")

# Scan a directory
result = aks.scan("./src")

# Scan the full project
result = aks.scan(".")

# Check for high-risk secrets
if result.has_high_risk:
    print(f"โŒ {result.high_count} HIGH-risk secrets found!")
else:
    print("โœ… No high-risk secrets found.")

# Access findings
for finding in result.findings:
    print(f"[{finding.severity}] {finding.type} in {finding.file}:{finding.line}")

# Save a JSON report
result.save_json("reports/report.json")

๐Ÿ–ฅ๏ธ CLI Examples

# Full project scan with verbose output
apikeyscanner scan . --verbose

# Scan and fail CI if HIGH secrets found (exit code 1)
apikeyscanner scan . --severity HIGH --json && echo "Safe" || echo "SECRETS FOUND"

# Scan a .env file
apikeyscanner scan .env

# Check version
apikeyscanner version

๐Ÿ“Š Example Terminal Output

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚            API Key Scanner               โ”‚
โ”‚    Local Secret Leak Detection Tool      โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

  Target:  ./myproject

  Found 4 possible secret(s)

 Severity   Type                File                  Line   Match
 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 HIGH       OpenAI API Key      config.py             12     sk-a********890
 HIGH       AWS Access Key ID   .env                  4      AKIA********XMP
 MEDIUM     Hardcoded Token     backend/auth.py       33     tok-a********456
 MEDIUM     Database URL        docker-compose.yml    18     post********3/db

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Summary โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚  Scanned files:   58                                  โ”‚
โ”‚  Skipped files:   12                                  โ”‚
โ”‚  High findings:   2                                   โ”‚
โ”‚  Medium findings: 2                                   โ”‚
โ”‚  Low findings:    0                                   โ”‚
โ”‚                                                       โ”‚
โ”‚  Security Status: FAILED โŒ                           โ”‚
โ”‚  Fix the detected secrets before pushing or deploying.โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

๐Ÿ Python Library API

import apikeyscanner as aks

result = aks.scan(
    path=".",
    severity=["HIGH", "MEDIUM"],   # filter by severity
    ignore=["node_modules", "venv"],
    recursive=True,
)

# Properties
result.total_findings    # int: total number of secrets found
result.high_count        # int: count of HIGH severity findings
result.medium_count      # int: count of MEDIUM severity findings
result.low_count         # int: count of LOW severity findings
result.has_findings      # bool: True if any secrets found
result.has_high_risk     # bool: True if any HIGH findings
result.is_clean          # bool: True if no secrets found
result.scan_mode         # str: "file", "directory", or "project"
result.scanned_files     # int: number of files scanned
result.skipped_files     # int: number of files skipped

# Methods
result.summary           # dict: compact summary
result.to_dict()         # dict: full result as dictionary
result.to_json()         # str: full result as JSON string
result.save_json("path") # save report to disk

๐Ÿ”— FastAPI Integration

from fastapi import FastAPI, HTTPException
import apikeyscanner as aks

app = FastAPI()

@app.post("/security/scan")
def scan_project():
    result = aks.scan(
        path="/srv/backend-app",
        severity=["HIGH"],
        ignore=["venv", "node_modules"],
    )

    if result.has_high_risk:
        raise HTTPException(
            status_code=403,
            detail={
                "message": "Deployment blocked. Secrets detected.",
                "findings": [f.to_dict() for f in result.findings],
            }
        )

    return {"message": "Safe to deploy.", "summary": result.summary}

๐Ÿงช Running Tests

# Install dev dependencies
pip install -e ".[dev]"

# Run all tests
pytest

# Run with coverage
pytest --cov=apikeyscanner

# Run a specific test class
pytest tests/test_scanner.py::TestOpenAIKeyDetection -v

๐Ÿ“ Project Structure

apikeyscanner/
โ”œโ”€โ”€ apikeyscanner/
โ”‚   โ”œโ”€โ”€ __init__.py      # Public API: exposes scan()
โ”‚   โ”œโ”€โ”€ scanner.py       # Core scanning engine
โ”‚   โ”œโ”€โ”€ patterns.py      # All detection patterns (regex + metadata)
โ”‚   โ”œโ”€โ”€ result.py        # ScanResult and Finding classes
โ”‚   โ”œโ”€โ”€ reporter.py      # JSON report generation
โ”‚   โ”œโ”€โ”€ cli.py           # Typer CLI + Rich terminal UI
โ”‚   โ”œโ”€โ”€ logger.py        # Structured logging
โ”‚   โ””โ”€โ”€ utils.py         # File filtering, masking, path helpers
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ test_scanner.py  # Pytest test suite
โ”‚   โ””โ”€โ”€ sample_files/    # Test fixtures
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ basic_usage.py   # Library usage examples
โ”‚   โ””โ”€โ”€ fastapi_usage.py # FastAPI integration
โ”œโ”€โ”€ reports/             # Generated reports (gitignored)
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ DOCUMENTATION.md
โ”œโ”€โ”€ pyproject.toml
โ””โ”€โ”€ requirements.txt

๐Ÿ›ก๏ธ Severity Levels

Level Color Meaning
HIGH ๐Ÿ”ด Red Critical secrets: API keys, passwords, private keys. Rotate immediately.
MEDIUM ๐ŸŸก Yellow Tokens, URLs with credentials, JWT tokens. Review and move to env vars.
LOW ๐Ÿ”ต Cyan Informational patterns that may indicate sensitive configuration.

โš ๏ธ Ethical Note

This tool is defensive only. It is designed to protect your own projects.

  • It only scans local files on your own machine.
  • It does not send data to any server.
  • It does not exploit or exfiltrate secrets.
  • It masks secret values in all output.
  • It is intended for use by developers, DevOps teams, and security teams to protect their own codebases.

Never use this tool on files or systems you do not own or have explicit permission to scan.


๐Ÿ‘ค Author


๐Ÿ“„ License

MIT ยฉ devxyasir

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

apikeyscanner-1.0.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apikeyscanner-1.0.0-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file apikeyscanner-1.0.0.tar.gz.

File metadata

  • Download URL: apikeyscanner-1.0.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for apikeyscanner-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2bcb662ff6756f7a1cd1139f136f405d48ad7d857a645cec132425571f56d2e8
MD5 a6afa722766b8abc3e099e64500ba0e9
BLAKE2b-256 a42377e502cc6eb06fc0ee161c21c7d37638a89b0efd9054a5bceaa4c822d8ec

See more details on using hashes here.

File details

Details for the file apikeyscanner-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: apikeyscanner-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for apikeyscanner-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70605abc185188d8a6c2d52404cff9742691637630c1dff312971129bed03f7c
MD5 f0c2341568b9bdc69f8c384f1dfe06cc
BLAKE2b-256 b7ecdd1e904d4ee59970405e6d8f489ccbde60829dff60bd429d017766b653f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page