{"id":3897,"library":"azure-mgmt-commerce","title":"Microsoft Azure Commerce Management Client Library for Python","description":"The azure-mgmt-commerce library provides a Python interface for managing Azure Commerce resources, primarily for retrieving usage details and rate card information via Azure Resource Manager (ARM) APIs. It is part of the broader Azure SDK for Python and is actively maintained with a regular release cadence. The current stable version is 6.0.0.","status":"active","version":"6.0.0","language":"en","source_language":"en","source_url":"https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/commerce/azure-mgmt-commerce","tags":["azure","cloud","management","commerce","billing","consumption","cost management"],"install":[{"cmd":"pip install azure-mgmt-commerce azure-identity","lang":"bash","label":"Install with Azure Identity"}],"dependencies":[{"reason":"Required for modern Azure Active Directory authentication in SDK v6.0.0+.","package":"azure-identity","optional":false}],"imports":[{"symbol":"CommerceManagementClient","correct":"from azure.mgmt.commerce import CommerceManagementClient"},{"symbol":"UsageManagementClient","correct":"from azure.mgmt.commerce import UsageManagementClient"},{"note":"Azure SDK v6.0.0+ deprecated azure.common.credentials and requires azure-identity for authentication.","wrong":"from azure.common.credentials import UserPassCredentials","symbol":"DefaultAzureCredential","correct":"from azure.identity import DefaultAzureCredential"}],"quickstart":{"code":"import os\nfrom datetime import date, timedelta\nfrom azure.identity import DefaultAzureCredential\nfrom azure.mgmt.commerce import UsageManagementClient\n\n# Set your Azure Subscription ID\n# You can set this as an environment variable or replace directly\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 or replace 'YOUR_SUBSCRIPTION_ID' in the code.\")\nelse:\n    try:\n        # Authenticate using DefaultAzureCredential (supports various auth methods)\n        credential = DefaultAzureCredential()\n\n        # Create a CommerceManagementClient\n        commerce_client = UsageManagementClient(credential, subscription_id)\n\n        # Get usage aggregates for the last day\n        end_date = date.today()\n        start_date = end_date - timedelta(days=1)\n\n        print(f\"Retrieving usage aggregates from {start_date} to {end_date}...\")\n        usage_iterator = commerce_client.usage_aggregates.list(\n            str(start_date) + 'T00:00:00Z',\n            str(end_date) + 'T00:00:00Z'\n        )\n\n        print(\"\\nDaily Usage Aggregates:\")\n        for item in usage_iterator:\n            print(f\"  Resource: {item.properties.meter_details.meter_name}, Quantity: {item.properties.quantity}, Unit: {item.properties.unit_of_measure}\")\n\n        # Example: Get Rate Card for a specific offer and region\n        # OfferDurableID example: 'MS-AZR-0062P' for Pay-As-You-Go\n        # This query string needs to be carefully constructed based on desired filters\n        # For available OfferDurableIDs, refer to Azure documentation\n        # Example query string for a specific offer, currency, locale, and region\n        rate_card_query = \"OfferDurableId eq 'MS-AZR-0062P' and Currency eq 'USD' and Locale eq 'en-US' and RegionInfo eq 'US'\"\n\n        print(f\"\\nGetting rate card for: {rate_card_query}\")\n        rate_card = commerce_client.rate_card.get(rate_card_query)\n\n        if rate_card.meters:\n            print(\"First 3 Rate Card Meters (if available):\")\n            for i, meter in enumerate(rate_card.meters):\n                if i >= 3: break\n                print(f\"  Meter: {meter.meter_name}, Unit: {meter.unit}, Tiered Rates: {meter.meter_rates}\")\n        else:\n            print(\"No meters found in the rate card for the given query.\")\n\n    except Exception as e:\n        print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential`, create a `UsageManagementClient`, and then retrieve daily usage aggregates and rate card information for your Azure subscription. Ensure you have the `AZURE_SUBSCRIPTION_ID` environment variable set and appropriate Azure permissions (e.g., Reader role on the subscription)."},"warnings":[{"fix":"Migrate your authentication code to use `azure-identity`. The `credentials` parameter in client constructors has also been renamed to `credential`.","message":"The credential system was completely revamped in version 6.0.0. Older `azure.common.credentials` or `msrestazure.azure_active_directory` are no longer supported. You must now use classes from the `azure-identity` library (e.g., `DefaultAzureCredential`).","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Remove `.config` access. Pass configuration like `enable_logging=True` directly to `CommerceManagementClient(credential, subscription_id, enable_logging=True)`.","message":"The `config` attribute on client objects was removed in version 6.0.0. Configuration options should now be passed directly as keyword arguments to the client's constructor.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Ensure you understand whether you need ARM (this library) or ASM (`azure-servicemanagement-legacy`). For modern Azure environments, `azure-mgmt-commerce` is the correct choice.","message":"This library (`azure-mgmt-commerce`) is for Azure Resource Manager (ARM) APIs. If you are working with older Azure Service Management (ASM) APIs, you need the `azure-servicemanagement-legacy` library instead. Most new development should use ARM.","severity":"gotcha","affected_versions":"<6.0.0 (for legacy context), >=6.0.0 (current ARM)"},{"fix":"Update calls to long-running operations to use the `begin_` prefix and handle the `azure.core.polling.LROPoller` return type if applicable.","message":"Operations that previously returned a `msrest.polling.LROPoller` now return `azure.core.polling.LROPoller` and are typically prefixed with `begin_` (e.g., `create_or_update` becomes `begin_create_or_update`). While `azure-mgmt-commerce` may not have many long-running operations, this is a general breaking change across Azure SDK management libraries in v6.x+.","severity":"breaking","affected_versions":">=6.0.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}