{"id":1465,"date":"2018-07-19T22:25:45","date_gmt":"2018-07-19T14:25:45","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/iotdev\/?p=1465"},"modified":"2019-02-15T10:27:09","modified_gmt":"2019-02-15T02:27:09","slug":"call-azure-iot-hub-rest-apis-in-vs-code","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/iotdev\/call-azure-iot-hub-rest-apis-in-vs-code\/","title":{"rendered":"Use VS Code to call Azure IoT Hub REST APIs"},"content":{"rendered":"<p>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 <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=vsciot-vscode.azure-iot-toolkit\" target=\"_blank\" rel=\"noopener\">Azure IoT Toolkit extension<\/a> for Visual Studio Code, you could easily use IoT Hub REST APIs in VS Code, no extra toolchain needed! Let&#8217;s see how quick it is to send a device-to-cloud message to Azure IoT Hub!<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>If you do not have an IoT Hub, you could follow our <a href=\"https:\/\/github.com\/Microsoft\/vscode-azure-iot-toolkit\/wiki\/Quickstart-Node.js#create-an-iot-hub\">tutorial<\/a> to create IoT Hub and device.<\/li>\n<li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=vsciot-vscode.azure-iot-toolkit\" target=\"_blank\" rel=\"noopener\">Azure IoT Toolkit extension<\/a> to help you do all the things when develop with Azure IoT Hub<\/li>\n<li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=humao.rest-client\">REST Client extension<\/a> to help you send HTTP request in VS Code<\/li>\n<\/ul>\n<h2>Prepare HTTP request<\/h2>\n<p>In VS Code, create a file called <strong>d2c.http <\/strong>with below CURL request, and replace {iothub-name} and {device-id}:<\/p>\n<pre>curl --request POST \r\n  --url 'https:\/\/{iothub-name}.azure-devices.net\/devices\/{device-id}\/messages\/events?api-version=2018-06-30' \r\n  --header 'authorization: {sas-token}' \r\n  --data 'Hello IoT Hub!'\r\n<\/pre>\n<h2>Generate SAS token<\/h2>\n<p>Right-click your device and select\u00a0<strong>Generate SAS Token for Device.<\/strong> The SAS token is created and copied to clipboard, then replace {sas-token} <strong>d2c.http<\/strong> in the with the SAS token.<strong>\n<\/strong><\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-1505\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2018\/07\/genSasToken-2.png\" alt=\"\" width=\"414\" height=\"351\" \/><\/p>\n<h2>Send HTTP request<\/h2>\n<ol>\n<li>Right-click your device and select\u00a0<strong>Start Monitoring D2C Message <\/strong>to monitor the device-to-cloud message<strong>\n<\/strong><\/li>\n<li>In <strong>d2c.http<\/strong> file, click &#8216;Send Request&#8217; link to call Azure IoT Hub REST API to send a d2c message<\/li>\n<li>In right response area, you could see the HTTP response which is HTTP 204 meaning the message is sent successfuly<\/li>\n<li>In the Output Channel of Azure IoT Toolkit, you could see the IoT Hub receive the &#8216;Hello IoT Hub!&#8217; message<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-1525\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2018\/07\/http.png\" alt=\"\" width=\"1169\" height=\"603\" srcset=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2018\/07\/http.png 1169w, https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2018\/07\/http-300x155.png 300w, https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2018\/07\/http-768x396.png 768w, https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2018\/07\/http-1024x528.png 1024w\" sizes=\"(max-width: 1169px) 100vw, 1169px\" \/><\/p>\n<p>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 <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=vsciot-vscode.azure-iot-toolkit\" target=\"_blank\" rel=\"noopener\">Azure IoT Toolkit extension<\/a> helps you to develop with Azure IoT Hub, checkout our <a href=\"https:\/\/github.com\/Microsoft\/vscode-azure-iot-toolkit\/wiki\">Wiki Page<\/a> to see the full features and tutorials.<\/p>\n<p>Useful Resources:<\/p>\n<ul>\n<li><a href=\"https:\/\/channel9.msdn.com\/Shows\/Internet-of-Things-Show\/Azure-IoT-Toolkit-extension-for-Visual-Studio-Code\" target=\"_blank\" rel=\"noopener\">Channel 9 video: Walkthrough of Azure IoT Toolkit extension<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-vscode-iot-toolkit-cloud-device-messaging\" rel=\"nofollow\">Use Azure IoT Toolkit to send and receive messages between your device and IoT Hub<\/a><\/li>\n<li><a href=\"https:\/\/blogs.msdn.microsoft.com\/iotdev\/2017\/09\/01\/handy-tool-when-you-develop-with-azure-iot\/\" target=\"_blank\" rel=\"noopener\">Handy Tool When You Develop With Azure IoT<\/a><\/li>\n<li><a href=\"https:\/\/blogs.msdn.microsoft.com\/iotdev\/2018\/07\/12\/use-vs-code-as-iot-hub-device-simulator-say-hello-to-azure-iot-hub-in-5-minutes\/\">Use VS Code as IoT Hub Device Simulator<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Microsoft\/vscode-azure-iot-toolkit\/wiki\/Quickstart-Node.js\">Create and control an IoT device connected to an IoT hub (Node.js)<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Microsoft\/vscode-azure-iot-toolkit\/wiki\/Quickstart-.NET\">Create and control an IoT device connected to an IoT hub (.NET)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s see how quick it [&hellip;]<\/p>\n","protected":false},"author":672,"featured_media":1860,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,17,29,33],"class_list":["post-1465","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-iot-dev","tag-azure-iot-hub","tag-iot","tag-rest-api","tag-visual-studio-code"],"acf":[],"blog_post_summary":"<p>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&#8217;s see how quick it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/posts\/1465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/users\/672"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/comments?post=1465"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/posts\/1465\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/media\/1860"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/media?parent=1465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/categories?post=1465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/tags?post=1465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}