### GitGuardian API Self-Hosted Base URL Example Source: https://docs.gitguardian.com/api-docs/api-reference Demonstrates the alternative base URL for GitGuardian API routes when using a self-hosted instance. This is crucial for correctly targeting API requests in a private deployment. ```text https://dashboard.gitguardian.mycorp.local/exposed/v1/scan ``` -------------------------------- ### Python SDK for GitGuardian API Source: https://docs.gitguardian.com/api-docs/usage-and-quotas This snippet demonstrates how to use the py-gitguardian Python SDK to interact with the GitGuardian API. It covers authentication and making API calls for scanning purposes. Ensure you have the SDK installed (`pip install py-gitguardian`). ```Python from gitguardian import GitGuardian # Initialize the GitGuardian client with your API key gitguardian_client = GitGuardian(api_key="YOUR_API_KEY") # Example: Scan a piece of text scan_result = gitguardian_client.scan("This is a test string with a potential secret: sk_test_abcdef1234567890") # Process the scan result if scan_result.secrets: print("Secrets found:") for secret in scan_result.secrets: print(f"- Type: {secret.type}, Match: {secret.match}") else: print("No secrets found.") # Example: Perform a multi-scan (up to 20 documents) texts_to_scan = [ "Document 1 content", "Document 2 content with another secret: pk_live_zyxwvu9876543210" ] multi_scan_result = gitguardian_client.multiscan(texts_to_scan) if multi_scan_result.secrets: print("Secrets found in multi-scan:") for secret in multi_scan_result.secrets: print(f"- Document: {secret.filename}, Type: {secret.type}, Match: {secret.match}") else: print("No secrets found in multi-scan.") ``` -------------------------------- ### Paginate GitGuardian API Members with Python Source: https://docs.gitguardian.com/api-docs/pagination This Python script demonstrates how to fetch all members from the GitGuardian API using cursor-based pagination. It iteratively requests data, appending results until the 'next' link is no longer present in the response headers. ```Python import os import requests BASE_URL ="https://api.gitguardian.com/v1" API_KEY = os.environ['GITGUARDIAN_API_TOKEN'] HEADERS ={"Authorization":f"Token {API_KEY}"} endpoint_url =f"{BASE_URL}/members?per_page=10" all_members =[] whileTrue: response = requests.get(endpoint_url, headers=HEADERS) assert response.status_code ==200, response.json() all_members += response.json() if"next"notin response.links: # final page was reached break endpoint_url = response.links["next"]["url"] ``` -------------------------------- ### Paginate GitGuardian API Members with Bash/cURL Source: https://docs.gitguardian.com/api-docs/pagination This Bash script utilizes cURL and jq to paginate through GitGuardian API member data. It repeatedly fetches data, extracts the 'next' URL from the Link header, and concatenates the results until all pages are retrieved. ```Bash #!/bin/bash URL="https://api.gitguardian.com/v1/members?per_page=10" # Check if jq is installed if ! command -v jq &> /dev/null; echo "jq could not be found, please install it to parse JSON." exit 1 fi MEMBERS="[]" while [ "${URL}" ]; do RESP=$(curl -i -Ss -H "Authorization: Token $API_KEY" "${URL}") # Check for curl error if [ $? -ne 0 ]; then echo "Error: Failed to retrieve data from ${URL}" exit 1 fi # Extract HTTP status code HTTP_STATUS=$(echo "${RESP}" | grep HTTP | awk '{print $2}') if [ "$HTTP_STATUS" != "200" ]; then echo "Error: Received HTTP status $HTTP_STATUS" exit 1 fi # Retrieve the body of the response and parse it with jq BODY=$(echo "${RESP}" | sed -n '/^\r$/,$p' | sed '1d' | jq '.') # Append to members list MEMBERS=$(echo "${MEMBERS}" | jq ". + ${BODY}") # Retrieve the next url from the response's headers URL=$(echo "${RESP}" | grep -i '^link:' | sed -n -E 's/^link:.*<(.*)>; rel="next".*/\1/p') done # Output the members list formatted with jq echo "${MEMBERS}" | jq '.' ``` -------------------------------- ### Manage GitGuardian Service Accounts Source: https://docs.gitguardian.com/api-docs/home Create and manage service accounts for API access. Service accounts represent non-human users for authentication and authorization, suitable for secrets scanning in CI pipelines or batch processing open incidents. ```text Service Account Management: Create, list, update, and delete service accounts via the API to manage programmatic access. ``` -------------------------------- ### Manage GitGuardian Personal Access Tokens Source: https://docs.gitguardian.com/api-docs/home Create and manage personal access tokens for API access. These tokens are typically used by developers on their local workstations for scanning secrets with tools like GitGuardian CLI (ggshield) in pre-commit or pre-push git hooks. ```text Personal Access Token Management: Generate, revoke, and manage personal access tokens for individual developer use. ``` -------------------------------- ### Authenticate GitGuardian API Requests Source: https://docs.gitguardian.com/api-docs/home Learn how to authenticate API requests using an API key, which can be either a service account or a personal access token. This is a fundamental step for interacting with the GitGuardian API. ```text API Key Authentication: Use your service account or personal access token in the Authorization header as a Bearer token. ``` -------------------------------- ### Authenticate GitGuardian API Request with Curl Source: https://docs.gitguardian.com/api-docs/authentication Demonstrates how to authenticate an API request to the GitGuardian `/health` endpoint using a `curl` command. It shows the required `Authorization` header format with a `Token` prefix and the API key. ```bash curl -H "Authorization: Token ${TOKEN}" \ https://api.gitguardian.com/v1/health ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.