Azure Management Consumption Client Library for Python
This is the Microsoft Azure Consumption Client Library. It allows programmatic management of consumption resources for Azure Enterprise Subscriptions, providing APIs to retrieve usage details, budgets, reservations, and other cost management data. The library is currently at version 10.0.0 and receives regular updates as part of the broader Azure SDK for Python.
Warnings
- breaking Credential system has been completely revamped. `azure.common.credentials` or `msrestazure.azure_active_directory` instances are no longer supported. Use classes from `azure-identity` (e.g., `DefaultAzureCredential`). The `credentials` parameter on client constructors has been renamed to `credential`.
- breaking Model signatures now use only keyword-argument syntax. All positional arguments for model instantiation must be rewritten as keyword arguments. This is a general Azure SDK change.
- breaking Version 10.0.0 introduced several breaking changes including: `BudgetFilter` no longer has parameter `not_property`. Operations like `ReservationRecommendationDetailsOperations.get`, `ReservationRecommendationsOperations.list`, `ReservationsDetailsOperations.list`, and `ReservationsSummariesOperations.list` now have a `resource_scope` parameter and in some cases no longer have the `scope` parameter.
- gotcha When listing usage details for Microsoft Customer Agreement (MCA) Pay-as-you-go subscriptions, users may find historical data restricted to only the current billing month, even with appropriate 'Reader' roles. This might be an API limitation for certain subscription types.
- gotcha Asynchronous clients are available in the `aio` namespace (e.g., `azure.mgmt.consumption.aio.ConsumptionManagementClient`). Using these requires an `async`/`await` pattern and an async credential.
Install
-
pip install azure-mgmt-consumption azure-identity
Imports
- ConsumptionManagementClient
from azure.mgmt.consumption import ConsumptionManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
- aio.ConsumptionManagementClient
from azure.mgmt.consumption.aio import ConsumptionManagementClient
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.consumption import ConsumptionManagementClient
# Set your Azure Subscription ID as an environment variable or replace directly
# For example: export AZURE_SUBSCRIPTION_ID="<your-subscription-id>"
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
if not subscription_id:
raise ValueError("AZURE_SUBSCRIPTION_ID environment variable not set.")
# Authenticate using DefaultAzureCredential
# This will try several authentication methods, including environment variables,
# managed identity, Visual Studio Code, Azure CLI, etc.
credential = DefaultAzureCredential()
# Create a client for the Azure Consumption Management
client = ConsumptionManagementClient(credential, subscription_id)
print(f"Listing usage details for subscription: {subscription_id}")
# List usage details for the current subscription
# 'scope' is typically '/subscriptions/{subscriptionId}' or '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}'
# For simplicity, we'll use the subscription scope.
scope = f"/subscriptions/{subscription_id}"
# Iterate through the usage details (adjust filter if needed)
# Note: For MCA (Microsoft Customer Agreement) subscriptions, historical data might be limited.
# See warnings for more details.
for usage_detail in client.usage_details.list(scope=scope, expand="meterDetails,additionalProperties", filter="properties/usageEnd ge '2026-03-01' and properties/usageEnd le '2026-03-31'", top=10):
print(f" Resource: {usage_detail.resource_id.split('/')[-1]} | Date: {usage_detail.usage_start} - {usage_detail.usage_end} | Cost: {usage_detail.pretax_cost} {usage_detail.currency}")
print("Successfully listed some usage details.")