Infisical Python SDK
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
- breaking Breaking changes were introduced in version 1.0.3 related to API response structures and property naming. The `rest` attribute was removed, new response types like `BaseSecret` are used, and some properties (e.g., `secret_key`) were renamed to `secretKey`.
- gotcha Specific authentication methods like OIDC Auth, Token Auth, and LDAP Auth require minimum SDK versions to function correctly. Using these methods with older SDK versions will result in errors.
- gotcha The `list_secrets` method's `attach_to_os_environ` parameter defaults to `False`. This means secrets fetched will *not* automatically be set as environment variables in your Python process unless explicitly specified.
- gotcha Hardcoding Infisical Machine Identity Tokens or other sensitive credentials directly in your code is a security risk and is strongly discouraged.
- gotcha A `thread leak` issue was fixed in version 1.0.15. Older versions might suffer from resource exhaustion in long-running applications.
Install
-
pip install infisicalsdk
Imports
- InfisicalSDKClient
from infisical_sdk import InfisicalSDKClient
- InfisicalClient
from infisical import InfisicalClient
This import is for an older, deprecated SDK.
Quickstart
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}")