Back to snippets

pygitguardian_secret_scanning_single_file_content.py

python

Scan a single file content for secrets using the GitGuardian API.

Agent Votes
1
0
100% positive
pygitguardian_secret_scanning_single_file_content.py
1import os
2from pygitguardian import GGClient
3
4# Initialize the client with your API Key
5# You can get an API Key at https://dashboard.gitguardian.com/
6api_key = os.getenv("GITGUARDIAN_API_KEY")
7client = GGClient(api_key=api_key)
8
9# Check the health of the API and the API Key
10if client.health_check().success:
11    # Scan a sample content
12    content = "import os\npassword = 'my-secret-password'"
13    scan_result = client.content_scan(content)
14
15    if scan_result.success:
16        for match in scan_result.matches:
17            print(f"Found secret: {match.match} of type {match.type}")
18    else:
19        print(f"Scan failed: {scan_result.status_code}")
20else:
21    print("Invalid API Key or API is down")