Registry / azure / azure-iot-hub

azure-iot-hub

JSON →
library2.7.0pypypiunverified

The `azure-iot-hub` library is part of the Microsoft Azure SDK for Python, providing capabilities to manage IoT Hub resources and interact with connected devices from a backend service. This includes managing device identities, sending cloud-to-device messages, and invoking direct methods on devices. The current version is 2.7.0, and Azure SDKs typically follow a frequent release cadence, with minor updates and bug fixes released regularly.

pip install azure-iot-hub
INSTALL
IMPORT
SIG · AZURE-IOT-HUB
A
azure-iot-hub
azurepythonv2.7.0
Install
3.0s avg
Import
Disk
42MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
build_error
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.0s · import 0.000s · 43MB
42MB installed
● package 42MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

IoTHubRegistryManager
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub import IoTHubRegistryManager

This quickstart demonstrates how to initialize the `IoTHubRegistryManager` using an IoT Hub connection string (preferably from an environment variable for security) and then create or retrieve a device identity within the IoT Hub. It also highlights the importance of closing the manager instance.

import os from azure.iot.hub import IoTHubRegistryManager # Retrieve IoT Hub connection string from environment variables for security IOT_HUB_CONNECTION_STRING = os.environ.get("AZURE_IOT_HUB_CONNECTION_STRING", "") if not IOT_HUB_CONNECTION_STRING: raise ValueError("AZURE_IOT_HUB_CONNECTION_STRING environment variable not set.") DEVICE_ID = "myPythonManagedDevice" manager = None try: # Create an instance of the IoTHubRegistryManager # This connection string should be for a service/iothubowner policy manager = IoTHubRegistryManager.from_connection_string(IOT_HUB_CONNECTION_STRING) print(f"Attempting to create device: {DEVICE_ID}") # Create a new device identity # If the device already exists, this will raise an IoTHubError (409 Conflict) try: device = manager.create_device(DEVICE_ID) print(f"Device '{device.device_id}' created successfully.") except Exception as e: if "409001 DeviceAlreadyExists" in str(e): print(f"Device '{DEVICE_ID}' already exists. Retrieving it.") device = manager.get_device(DEVICE_ID) print(f"Retrieved device '{device.device_id}'.") else: raise e # You can inspect the device object, e.g., its primary key if device.authentication and device.authentication.symmetric_key: print(f"Primary key: {device.authentication.symmetric_key.primary_key}") print("Successfully interacted with IoT Hub Registry.") finally: # Close the manager to release resources if manager: print("Closing IoTHubRegistryManager...") manager.close() print("Manager closed.")
Debug
Known issues
breakingMigration from older V1 Python clients like `azure-iothub-service-client` to the V2 SDK `azure-iot-hub` involves significant breaking changes. The package name, module structure (e.g., `azure.iot.hub` vs `azure.iothub`), class names, and API signatures are completely different. Code written for V1 clients will not work with `azure-iot-hub` without extensive modifications.
fix
Rewrite code following the V2 `azure-iot-hub` SDK patterns. Refer to the official Azure SDK documentation and migration guides for updated import paths and API usage.
affects: < 2.0.0 (old clients) vs >= 2.0.0 (azure-iot-hub)
gotchaIt's crucial to distinguish between `azure-iot-hub`, `azure-iot-device`, and `azure-iot-provisioningservice`. `azure-iot-hub` is for backend *service-side* operations (managing devices, sending C2D messages). `azure-iot-device` is for *device-side* operations (sending D2C messages, receiving C2D messages). `azure-iot-provisioningservice` is for interacting with the Device Provisioning Service. Using the wrong library for a task is a common mistake.
fix
Ensure you are using the correct library for your specific use case. `azure-iot-hub` for service interactions, `azure-iot-device` for device interactions, and `azure-iot-provisioningservice` for DPS management.
affects: All versions
gotchaThe `IoTHubRegistryManager.from_connection_string` method requires an IoT Hub *service* connection string (e.g., from an `iothubowner` or `service` policy), not a device connection string. Using a device connection string will result in authentication errors.
fix
When generating the connection string from the Azure portal, select 'Shared access policies' under 'Security settings' and copy the connection string for a policy like 'iothubowner' or 'service'.
affects: All versions
gotchaThe `IoTHubRegistryManager` instance holds network connections. Failing to call `manager.close()` when you are finished with it can lead to resource leaks and prevent your application from cleanly exiting, especially in long-running processes or serverless functions.
fix
Always ensure `manager.close()` is called when the manager is no longer needed. The recommended pattern is to use a `try...finally` block to guarantee closure, as shown in the quickstart example.
affects: All versions
Upgrade
Version history
2.7.0latest on PyPI · released Aug 6, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
49 hits · last 30 days
node
38
OpenAI (training)
4
Resources