pygitguardian
pygitguardian is the official Python client library for the GitGuardian API, allowing developers to detect over 200 types of secrets and other security vulnerabilities in text content. It is actively developed with frequent releases, currently at version 1.29.0.
Warnings
- breaking Version 1.20.0 removed support for the deprecated SCA (Software Composition Analysis) and IaC (Infrastructure as Code) scanning endpoints.
- breaking Version 1.25.0 updated the `Client.scan_and_create_incidents()` method to align with server-side API changes. This may require reviewing and updating parameters or expected behavior if this method was previously used.
- gotcha In version 1.27.0, several fields including `visibility`, `kind`, `presence status`, `ignore_reason`, and `tag` were changed to `str` type. Code expecting different types (e.g., enums or custom objects) might encounter type errors.
- gotcha As of version 1.23.0, the `detector_name` and `detector_group_name` fields in `PolicyBreak` objects were made optional. Code expecting these fields to always be present should implement checks for `None`.
Install
-
pip install pygitguardian
Imports
- GGClient
from pygitguardian import GGClient
- Detail
from pygitguardian.models import Detail
Quickstart
import os
from pygitguardian import GGClient
from pygitguardian.models import ScanResult
# It is highly recommended to use environment variables for API keys.
# Replace 'YOUR_GITGUARDIAN_API_KEY' with your actual environment variable name if different.
api_key = os.environ.get('GITGUARDIAN_API_KEY', '')
if not api_key:
print("Error: GITGUARDIAN_API_KEY environment variable not set.")
exit(1)
client = GGClient(api_key=api_key)
document_to_scan = """
This is some example content.
It might contain sensitive information like a password: mysecretpassword123
or an API key: sk_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
"""
try:
# Check the health of the API and the API key used first
health_check_result = client.health_check()
if not health_check_result.success:
print(f"API Health Check failed: {health_check_result.detail}")
exit(1)
print("API Health Check successful.")
scan_result: ScanResult = client.content_scan(document_to_scan)
if scan_result.has_secrets:
print("Secrets found in the document!")
for incident in scan_result.incidents:
print(f" Incident type: {incident.type}")
for pb in incident.policy_breaks:
print(f" Policy Break: {pb.occurrence.value}")
else:
print("No secrets found in the document.")
except Exception as e:
print(f"An error occurred during scan: {e}")