Azure Cosmos DB Management Client Library for Python

9.9.0 · active · verified Mon Apr 06

The Microsoft Azure Cosmos DB Management Client Library for Python provides capabilities to manage Azure Cosmos DB resources, such as creating, updating, and deleting Cosmos DB accounts, databases, and containers. It interacts with the Azure Cosmos DB resource provider (control plane). The current version is 9.9.0, and it generally follows a regular release cadence as part of the Azure SDK for Python updates.

Warnings

Install

Imports

Quickstart

Initializes the `CosmosDBManagementClient` using `DefaultAzureCredential` and lists existing Cosmos DB accounts. Ensure `AZURE_SUBSCRIPTION_ID` and other Azure authentication environment variables (like `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`) are set.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.cosmosdb import CosmosDBManagementClient

# Set these environment variables for authentication
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET for service principal
# AZURE_SUBSCRIPTION_ID

subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")

if subscription_id == "YOUR_SUBSCRIPTION_ID":
    print("Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'YOUR_SUBSCRIPTION_ID' in the code.")
    exit()

# Authenticate using DefaultAzureCredential
# This will try to authenticate using various methods (env vars, managed identity, CLI, etc.)
credential = DefaultAzureCredential()

# Create a CosmosDB management client
client = CosmosDBManagementClient(credential, subscription_id)

# Example: List all Cosmos DB accounts in the subscription
print("Listing Cosmos DB accounts...")
for account in client.database_accounts.list():
    print(f" - {account.name} (Resource Group: {account.resource_group})")

print("Client initialized successfully and listed accounts.")

view raw JSON →