Blogs are 1% about sharing information with others and 99% about sharing information with your self and this post is no different. From time to time I have to generate a SAS token for either connecting to IoT Hub from a device or service. The Azure IoT Hub CLI allows you to generate SAS tokens for most scenarios expect for one: When you only have the device name, host and key. I am not sure why this option is not available via the CLI but lucky for us, there is an easy way around it.
When you use any of the SDKs to connect to IoT Hub, the SDKs automatically generate the SAS token for you. Using the Node SDK, I created a simple Azure Function to help me generate a token.
All you need is the code bellow and the Node SDK ( npm i azure-iot-device ). You can pass the variables via the request body to make it easier to reuse.