Use VS Code to call Azure IoT Hub REST APIs

Jun Han

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

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

  1. Right-click your device and select Start Monitoring D2C Message to monitor the device-to-cloud message
  2. In d2c.http file, click ‘Send Request’ link to call Azure IoT Hub REST API to send a d2c message
  3. In right response area, you could see the HTTP response which is HTTP 204 meaning the message is sent successfuly
  4. 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:

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Edvin Sidebo 0

    Thanks for this post Jun. Can you help me understand if/how I can send a message from the cloud (app) to a device, with a similar curl request? As I understand it from this page https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-c2d there should be a /messages/devicebound endpoint to use? But how does one specify the device? (Currently I just want to try this in the terminal, not in VS code.) 

Feedback usabilla icon