Microsoft Azure Billing Client Library for Python
The azure-mgmt-billing library is the Microsoft Azure Billing Client Library for Python. It provides an interface for managing Azure billing resources. As part of the wider Azure SDK for Python, it adheres to modern SDK guidelines and receives regular updates to support new Azure features and improvements. The current stable version is 7.0.0.
Common errors
-
ModuleNotFoundError: No module named 'azure.mgmt.billing'
cause The 'azure-mgmt-billing' package is not installed in the current Python environment.fixRun `pip install azure-mgmt-billing` to install the library. -
azure.core.exceptions.DeserializationError: Unable to deserialize response data.
cause The Azure Billing API returned data in an unexpected format (e.g., date format) that the SDK's deserializer cannot parse, particularly noted when listing billing accounts with version 7.0.0.fixEnsure you are using the latest stable version of `azure-mgmt-billing` and `azure-core`. If the issue persists, review the specific API documentation for the operation or consider reporting it to the Azure SDK team if it appears to be a library bug. -
The resource or one of its dependencies could not be found. (Code: NotFound)
cause The specified Azure billing resource (e.g., billing account, invoice, subscription) does not exist, the resource ID is incorrect, or the authenticated identity is attempting to access a resource outside its permitted subscription or scope.fixVerify that the resource name, ID, and the subscription context used in your code are correct and that the resource actually exists and is accessible. Double-check the parameters passed to the API call. -
Operation returned an invalid status code 'Unauthorized'
-
AuthorizationFailed: The client '<client-id>' with object id '<object-id>' does not have authorization to perform action '<action>' over scope '<scope>' or the scope is invalid.
cause The Azure identity (user, service principal, or managed identity) used for authentication lacks the necessary Azure Role-Based Access Control (RBAC) permissions (e.g., 'Billing Reader' or 'Cost Management Reader') to perform the requested billing operation on the specified resource scope.fixAssign the appropriate RBAC role (e.g., 'Billing Reader' or 'Cost Management Reader') to the authenticated identity at the correct scope (subscription, billing account, or resource group) in the Azure portal.
Warnings
- breaking Version 7.0.0 introduced significant breaking changes, including the deletion or renaming of client operation groups such as 'BillingManagementClient.instructions', 'BillingManagementClient.billing_role_definitions', and 'BillingManagementClient.billing_periods'. Additionally, the 'Agreement' model had its 'agreement_link', 'category', 'acceptance_mode', and 'effective_date' instance variables deleted or renamed.
- breaking The Azure SDK for Python underwent a major regeneration around version 6.0.0. This change revamped the credential system (deprecating `azure.common.credentials` and `msrestazure.azure_active_directory` in favor of `azure-identity`), renamed the `credentials` parameter to `credential`, removed the `config` attribute from clients, and introduced the `begin_` prefix for long-running asynchronous operations (LROs).
- deprecated Support for Python 2.7 in Azure SDK Python packages officially ended on January 1, 2022. Using this library with Python 2.7 is unsupported and may lead to security vulnerabilities or unexpected behavior.
- gotcha Many resource management operations in the Azure SDK for Python (especially those involving creating, updating, or deleting resources) are asynchronous and return an `LROPoller` object. These methods are typically prefixed with `begin_`.
Install
-
pip install azure-mgmt-billing azure-identity
Imports
- BillingManagementClient
from azure.mgmt.billing import BillingManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.billing import BillingManagementClient
# Set these environment variables for authentication
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
# AZURE_SUBSCRIPTION_ID (for billing operations)
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
# Authenticate using DefaultAzureCredential
credential = DefaultAzureCredential()
# Create a BillingManagementClient
billing_client = BillingManagementClient(credential=credential, subscription_id=subscription_id)
# Example: List billing accounts (this operation might not require subscription_id directly,
# but the client is often initialized with it for consistency across management clients)
# Replace with an actual operation relevant to your billing scenario
# For example: print(list(billing_client.billing_accounts.list()))
print('BillingManagementClient initialized successfully.')