Infisical Python SDK

1.0.16 · active · verified Wed Apr 15

The `infisicalsdk` is the official Python client SDK for Infisical, an open-source, end-to-end encrypted platform for managing secrets and configurations. It enables Python applications to fetch secrets on demand from Infisical deployments (cloud or self-hosted). The library is actively maintained, with its latest version being 1.0.16, and receives regular updates and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `InfisicalSDKClient` and fetch secrets. It shows how to retrieve a single secret by name and how to list all secrets within a specified project, environment, and path. Authentication can be done via a direct service token or through Machine Identity credentials (e.g., Universal Auth).

import os
from infisical_sdk import InfisicalSDKClient
from infisical_sdk.models.shared import UniversalAuthLoginInput

# It's recommended to use environment variables for sensitive data like tokens
# For Universal Auth, you would typically use INFISICAL_UNIVERSAL_AUTH_CLIENT_ID and INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET
# Or a service token directly via INFISICAL_TOKEN
# For this example, we'll use a direct token, but env vars are preferred.
INFISICAL_TOKEN = os.environ.get('INFISICAL_TOKEN', 'your_infisical_service_token') # Replace with your actual token or set as env var
INFISICAL_PROJECT_ID = os.environ.get('INFISICAL_PROJECT_ID', 'your_project_id') # Replace with your project ID or set as env var
INFISICAL_ENVIRONMENT = os.environ.get('INFISICAL_ENVIRONMENT', 'dev') # Replace with your environment or set as env var

try:
    # Initialize the client. The 'token' parameter allows direct authentication.
    # Alternatively, you can use client.auth.universal_auth.login() with client_id/client_secret.
    client = InfisicalSDKClient(token=INFISICAL_TOKEN)

    # Fetch a single secret by its name
    secret = client.secrets.get_secret_by_name(
        secret_name='MY_APPLICATION_SECRET',
        project_id=INFISICAL_PROJECT_ID,
        environment_slug=INFISICAL_ENVIRONMENT,
        secret_path='/'
    )

    if secret and hasattr(secret, 'secret_value'):
        print(f"Fetched secret 'MY_APPLICATION_SECRET': {secret.secret_value}")
    else:
        print("Secret 'MY_APPLICATION_SECRET' not found or has no value.")

    # List all secrets in a specific path and environment
    all_secrets_response = client.secrets.list_secrets(
        project_id=INFISICAL_PROJECT_ID,
        environment_slug=INFISICAL_ENVIRONMENT,
        secret_path='/'
    )

    if all_secrets_response and all_secrets_response.secrets:
        print("\nAll secrets in root path:")
        for s in all_secrets_response.secrets:
            print(f"  - {s.secret_name}: {s.secret_value}")
    else:
        print("No secrets found in the specified path.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →