The REST APIs for IoT Hub offer programmatic access to the device, messaging, and job services, as well as the resource provider, in IoT Hub. With the Azure IoT Toolkit extension for Visual Studio Code, you could easily use IoT Hub REST APIs in VS Code, no extra toolchain needed! Let's see how quick it is to send a device-to-cloud message to Azure IoT Hub!
Prerequisites
- If you do not have an IoT Hub, you could follow our tutorial to create IoT Hub and device.
- Azure IoT Toolkit extension to help you do all the things when develop with Azure IoT Hub
- REST Client extension to help you send HTTP request in VS Code
Prepare HTTP request
In VS Code, create a file called d2c.http with below CURL request, and replace {iothub-name} and {device-id}:
curl --request POST --url 'https://{iothub-name}.azure-devices.net/devices/{device-id}/messages/events?api-version=2018-06-30' --header 'authorization: {sas-token}' --data 'Hello IoT Hub!'
Generate SAS token
Right-click your device and select Generate SAS Token for Device. The SAS token is created and copied to clipboard, then replace {sas-token} d2c.http in the with the SAS token.
Send HTTP request
- Right-click your device and select Start Monitoring D2C Message to monitor the device-to-cloud message
- In d2c.http file, click 'Send Request' link to call Azure IoT Hub REST API to send a d2c message
- In right response area, you could see the HTTP response which is HTTP 204 meaning the message is sent successfuly
- In the Output Channel of Azure IoT Toolkit, you could see the IoT Hub receive the 'Hello IoT Hub!' message
Without any extra toolchain, you could do all the things to call Azure IoT REST APIs in Visual Studio Code. If you want to learn more about how Azure IoT Toolkit extension helps you to develop with Azure IoT Hub, checkout our Wiki Page to see the full features and tutorials.
Useful Resources: