IBM Cloud Secrets Manager Python SDK

2.1.19 · active · verified Thu Apr 16

The `ibm-secrets-manager-sdk` is the official Python SDK for interacting with IBM Cloud Secrets Manager. It provides programmatic access to store, manage, and retrieve secrets like API keys, passwords, and certificates. Currently at version 2.1.19, the library maintains a frequent release cadence, often incorporating bug fixes, new secret types, and support for service features, as well as dependency updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `SecretsManagerV2` client using an IAM API key and then list the secrets available in your IBM Cloud Secrets Manager instance. Ensure `IBM_CLOUD_API_KEY` and `IBM_SECRETS_MANAGER_URL` environment variables are set with your credentials and service endpoint.

import os
from ibm_secrets_manager_sdk.secrets_manager_v2 import SecretsManagerV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

# Configure IBM Cloud API Key and Secrets Manager Service URL
# It is highly recommended to use environment variables for credentials.
api_key = os.environ.get("IBM_CLOUD_API_KEY", "YOUR_IBM_CLOUD_API_KEY")
service_url = os.environ.get("IBM_SECRETS_MANAGER_URL", "YOUR_SECRETS_MANAGER_URL") # e.g., 'https://<region>.secrets-manager.appdomain.cloud/api'

if api_key == "YOUR_IBM_CLOUD_API_KEY" or service_url == "YOUR_SECRETS_MANAGER_URL":
    print("WARNING: Please set IBM_CLOUD_API_KEY and IBM_SECRETS_MANAGER_URL environment variables or replace placeholders.")
    # For demonstration, we'll proceed, but real applications require valid credentials.
    # raise ValueError("Missing IBM_CLOUD_API_KEY or IBM_SECRETS_MANAGER_URL environment variable.")

# Initialize authenticator
authenticator = IAMAuthenticator(api_key)

# Initialize the Secrets Manager service client
secrets_manager_service = SecretsManagerV2(
    authenticator=authenticator
)
secrets_manager_service.set_service_url(service_url)

try:
    # Example: List up to 10 secrets in your instance
    list_secrets_response = secrets_manager_service.list_secrets(
        limit=10
    ).get_result()

    if list_secrets_response and list_secrets_response.resources:
        print(f"Successfully retrieved {len(list_secrets_response.resources)} secrets:")
        for secret in list_secrets_response.resources:
            print(f"- ID: {secret.id}, Name: {secret.name}, Type: {secret.secret_type}")
    else:
        print("No secrets found in the specified instance.")

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

view raw JSON →