Azure Key Vault Management Client

14.0.1 · active · verified Thu Apr 09

Microsoft Azure Keyvault Management Client Library for Python. It provides an interface to manage Azure Key Vault resources, such as creating, deleting, and updating vaults, and configuring access policies. Current version is 14.0.1. Releases follow the Azure SDK for Python's frequent cadence, often coinciding with new API versions or bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate using `DefaultAzureCredential` and list all Key Vaults within a specified Azure subscription using `KeyVaultManagementClient`. It assumes `AZURE_SUBSCRIPTION_ID` is set as an environment variable and appropriate permissions are granted.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.keyvault import KeyVaultManagementClient

# --- Authentication ---
# The DefaultAzureCredential attempts to authenticate via several methods,
# including environment variables, managed identity, Azure CLI, and more.
# For local development, set these environment variables or ensure 'az login' is active:
# - AZURE_SUBSCRIPTION_ID (required)
# - AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID for service principal
# - AZURE_USERNAME, AZURE_PASSWORD for developer accounts

subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID")
if not subscription_id:
    raise ValueError("AZURE_SUBSCRIPTION_ID environment variable must be set.")

credential = DefaultAzureCredential()

# --- Client Initialization ---
client = KeyVaultManagementClient(credential, subscription_id)

# --- Example: List all Key Vaults in the subscription ---
print(f"Listing all Key Vaults in subscription: {subscription_id}")
try:
    vaults_iterator = client.vaults.list()
    found_vaults = False
    for vault in vaults_iterator:
        print(f"  - Vault Name: {vault.name}, Location: {vault.location}")
        found_vaults = True
    if not found_vaults:
        print("  No Key Vaults found.")
except Exception as e:
    print(f"Error listing vaults: {e}")
    print("Ensure your credential has the 'Microsoft.KeyVault/vaults/read' permission at the subscription scope.")

view raw JSON →