{"id":5859,"library":"azure-mgmt-costmanagement","title":"Azure Cost Management Client Library","description":"The Microsoft Azure Cost Management Client Library for Python allows developers to manage Azure cost and usage data programmatically. It provides access to features like querying cost data, managing budgets, and working with dimensions and exports. Currently at version 4.0.1, it follows the Azure SDK release cadence, with updates often tied to new API versions and general SDK guidelines.","status":"active","version":"4.0.1","language":"en","source_language":"en","source_url":"https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/costmanagement/azure-mgmt-costmanagement","tags":["Azure","Cloud","Cost Management","SDK","Finance","Cost Optimization"],"install":[{"cmd":"pip install azure-mgmt-costmanagement azure-identity","lang":"bash","label":"Install core library and authentication"}],"dependencies":[],"imports":[{"symbol":"CostManagementClient","correct":"from azure.mgmt.costmanagement import CostManagementClient"},{"note":"Required for authentication with Azure services.","symbol":"DefaultAzureCredential","correct":"from azure.identity import DefaultAzureCredential"}],"quickstart":{"code":"import os\nfrom azure.identity import DefaultAzureCredential\nfrom azure.mgmt.costmanagement import CostManagementClient\n\n# Set your Azure Subscription ID from environment variables\nsubscription_id = os.environ.get(\"AZURE_SUBSCRIPTION_ID\", \"YOUR_SUBSCRIPTION_ID\")\n\nif subscription_id == \"YOUR_SUBSCRIPTION_ID\":\n    print(\"Please set the AZURE_SUBSCRIPTION_ID environment variable.\")\nelse:\n    try:\n        # Authenticate using DefaultAzureCredential (looks for env vars, managed identity, etc.)\n        credential = DefaultAzureCredential()\n\n        # Create a CostManagementClient\n        client = CostManagementClient(credential, subscription_id)\n\n        # Example: List budgets for the subscription\n        print(f\"Listing budgets for subscription: {subscription_id}\")\n        scope = f\"subscriptions/{subscription_id}\"\n        budgets = client.budgets.list(scope=scope)\n        \n        found_budgets = False\n        for budget in budgets:\n            print(f\"  Budget Name: {budget.name}, Amount: {budget.amount} {budget.currency}\")\n            found_budgets = True\n        \n        if not found_budgets:\n            print(\"  No budgets found for this subscription.\")\n\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n        print(\"Ensure you have set up Azure credentials (e.g., AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID) or are logged in via `az login`.\")\n","lang":"python","description":"This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and then list existing budgets within a specified Azure subscription using the `CostManagementClient`. Ensure you have the `AZURE_SUBSCRIPTION_ID` environment variable set, and your Azure credentials configured (e.g., via `az login` or environment variables like `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_TENANT_ID`)."},"warnings":[{"fix":"Update import paths and client instantiation logic. Refer to the official Azure SDK documentation for the new API surface.","message":"Version 3.0.0 introduced a complete rewrite of the SDK following new Azure SDK guidelines. The main client class was renamed to `CostManagementClient`, and all models and operations moved under `azure.mgmt.costmanagement.models` and `azure.mgmt.costmanagement.operations`. Asynchronous operations are now available via `azure.mgmt.costmanagement.aio`.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure that `time_period` and `type` are always provided when constructing `QueryDefinition` objects for queries.","message":"In version 4.0.0, the `QueryDefinition` model had two new required parameters added: `time_period` and `type`. Calls using `QueryDefinition` without these parameters will fail.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Always install `azure-identity` alongside any `azure-mgmt-*` library: `pip install azure-mgmt-costmanagement azure-identity`.","message":"Azure SDK management libraries like `azure-mgmt-costmanagement` require separate installation of an authentication library (e.g., `azure-identity`) for convenient credential management. While `azure-mgmt-costmanagement` does not directly depend on `azure-identity` via `pip` requirements, it is essential for practical use.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure the scope string is correctly formatted (e.g., `subscriptions/{subscriptionId}`, `subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}`) and the authenticated principal has the necessary 'Cost Management Contributor' or similar role.","message":"The Cost Management API often uses 'scopes' to define the context for operations (e.g., subscription, resource group, management group). Incorrectly formatted scopes or scopes without appropriate permissions will result in authorization errors.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}