{"id":1366,"library":"azure-mgmt-iothub","title":"Azure IoT Hub Management Client","description":"The `azure-mgmt-iothub` library is the Microsoft Azure IoT Hub Management Client Library for Python, enabling programmatic control and management of Azure IoT Hub resources. It allows for creating, updating, deleting, and querying IoT Hubs and their associated entities (like consumer groups and private endpoints). The current version is 4.0.0, and it follows the Azure SDK for Python's release cadence, with updates typically aligned with new Azure service features or platform changes.","status":"active","version":"4.0.0","language":"en","source_language":"en","source_url":"https://github.com/Azure/azure-sdk-for-python","tags":["azure","cloud","iot","management","sdk","microsoft"],"install":[{"cmd":"pip install azure-mgmt-iothub","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides shared primitives, exceptions, and operations for the Azure SDK. Required for HTTP pipeline and error handling.","package":"azure-core","optional":false},{"reason":"Provides Azure Active Directory authentication support for Azure SDK clients. Essential for authenticating with Azure services.","package":"azure-identity","optional":false}],"imports":[{"note":"The client class is directly exposed under the top-level package for simplified access.","wrong":"from azure.mgmt.iothub.iot_hub_client import IotHubClient","symbol":"IotHubClient","correct":"from azure.mgmt.iothub import IotHubClient"},{"note":"Older authentication methods like ServicePrincipalCredentials (from azure-common/msrestazure) have been replaced by the unified azure-identity package in v4.x and later.","wrong":"from azure.common.credentials import ServicePrincipalCredentials","symbol":"DefaultAzureCredential","correct":"from azure.identity import DefaultAzureCredential"}],"quickstart":{"code":"import os\nfrom azure.identity import DefaultAzureCredential\nfrom azure.mgmt.iothub import IotHubClient\n\n# Ensure AZURE_SUBSCRIPTION_ID is set in your environment variables\n# For local development, also set AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET\n# or use 'az login' for Azure CLI credential.\n\nsubscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'your-subscription-id') # Replace with your actual subscription ID or ensure env var is set\nif not subscription_id or subscription_id == 'your-subscription-id':\n    raise ValueError(\"Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'your-subscription-id' in the code.\")\n\n# Acquire a credential object using DefaultAzureCredential\n# This attempts various authentication methods (environment variables, managed identity, CLI, etc.)\ncredential = DefaultAzureCredential()\n\n# Construct an IoT Hub client\nclient = IotHubClient(credential, subscription_id)\n\n# Example: List all IoT Hubs in the subscription\nprint(\"Listing all IoT Hubs in the subscription...\")\niot_hubs = client.iot_hub_resource.list_by_subscription()\n\nfound_hubs = False\nfor hub in iot_hubs:\n    found_hubs = True\n    print(f\"  - Name: {hub.name}, Location: {hub.location}\")\n\nif not found_hubs:\n    print(\"No IoT Hubs found in this subscription.\")\n\n# Example: Get an IoT Hub (replace with actual resource group and hub name if you have one)\n# resource_group_name = os.environ.get('AZURE_RESOURCE_GROUP', 'my-resource-group')\n# hub_name = os.environ.get('AZURE_IOT_HUB_NAME', 'my-iothub')\n# try:\n#     single_hub = client.iot_hub_resource.get(resource_group_name, hub_name)\n#     print(f\"\\nRetrieved single IoT Hub: {single_hub.name} in {single_hub.location}\")\n# except Exception as e:\n#     print(f\"\\nCould not retrieve IoT Hub {hub_name}: {e}\")\n","lang":"python","description":"This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and list all IoT Hubs within your specified Azure subscription. Ensure your `AZURE_SUBSCRIPTION_ID` environment variable is set. For local development, `DefaultAzureCredential` will also pick up credentials from `az login` (Azure CLI)."},"warnings":[{"fix":"Update your authentication code to use `DefaultAzureCredential` or other credentials from the `azure-identity` package. The client constructor now typically takes `(credential, subscription_id)` directly, without an explicit 'base_url' parameter.","message":"Version 4.0.0 introduced significant breaking changes, migrating away from `msrestazure` based authentication and client construction. It now exclusively uses `azure-core` for HTTP operations and `azure-identity` for authentication.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Always iterate over the results of listing methods (e.g., `for item in client.iot_hub_resource.list_by_subscription(): ...`) to process them. If a full list is required, convert the iterator using `list()`: `all_hubs = list(client.iot_hub_resource.list_by_subscription())`.","message":"Listing operations (e.g., `list_by_subscription()`, `list_by_resource_group()`) return an iterator, not a complete list. You must iterate over the result to access individual resources. Attempting to index directly or treat it as a list will fail.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always refer to the method signature in the documentation for operations that might require a resource group name, and ensure it's provided as an argument where needed. For example, `client.iot_hub_resource.get(resource_group_name, hub_name)`.","message":"Many management operations (e.g., getting, creating, updating IoT Hubs) require a resource group name in addition to the subscription ID. Forgetting to provide this or providing an incorrect one will result in `ResourceNotFound` or `ValidationError` exceptions.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}