{"id":9498,"date":"2015-10-06T12:58:32","date_gmt":"2015-10-06T17:58:32","guid":{"rendered":"http:\/\/www.multitech.net\/developer\/?page_id=9498"},"modified":"2022-03-30T19:48:08","modified_gmt":"2022-03-31T00:48:08","slug":"node-red-and-rest-api-cloud-service","status":"publish","type":"page","link":"https:\/\/www.multitech.net\/developer\/software\/aep\/node-red-and-rest-api-cloud-service\/","title":{"rendered":"Connect Node-RED to Any Cloud Service&#8217;s REST API"},"content":{"rendered":"<p><strong>NOTE:\u00a0<\/strong>Support for Node-RED\/Node.js on Multitech AT91SAM9G25-based products has been discontinued within mPower 5.3. For mPower 5.3.3 to 5.3.8, users can\u00a0install Node-RED as a custom application.\u00a0<em><strong>mPower 6.0 or later no longer supports this custom app option.<\/strong>\u00a0<\/em><\/p>\n<p>The following instructions are still applicable for mPower MTCDT 5.2.1 and MTCAP 5.2.1 and earlier on all devices. And also applies to any non-AT91SAM9G25-based products.<\/p>\n<p>Refer to this alterative example using Python of a default application and server with API implementation for a distributed LoRaWAN network:<a href=\"https:\/\/github.com\/MultiTechSystems\/lorawan-app-connect\">\u00a0https:\/\/github.com\/MultiTechSystems\/lorawan-app-connect<\/a>.<\/p>\n<p>For more details on other methods and examples to create custom applications, see\u00a0<a href=\"https:\/\/www.multitech.net\/developer\/software\/aep\/creating-a-custom-application\/\">Creating a Custom Application<\/a>.<\/p>\n<p>Using the <strong>function<\/strong> and <strong>http request <\/strong>nodes the Conduit<sup>\u00ae\u00a0<\/sup>can be configured to communicate with any REST API cloud service.<\/p>\n<ol>\n<li>Drag the following nodes onto the sheet and connect them sequentially: <strong>inject<\/strong>, <strong>function<\/strong>, <strong>http request<\/strong>, and <strong>debug<\/strong><\/li>\n<li>Double click on the function node and enter the following example code with settings from your cloud service&#8217;s documentation. <b>Xively Example<\/b>\n<pre class=\"brush:js\">var apikey = \"&lt;YOUR-API-KEY&gt;\";\r\nvar feed_id = \"&lt;YOUR-FEED-ID&gt;\";\r\n\r\nvar data = {};\r\ndata[\"version\"] = \"1.0.0\";\r\ndata[\"datastreams\"] = [];\r\ndata[\"datastreams\"].push({\"id\":\"one\", \"current_value\":\"100.00\"});\r\nmsg.method = \"PUT\";\r\nmsg.headers = { \"X-ApiKey\": apikey };\r\nmsg.payload = JSON.stringify(data);\r\nmsg.url = \"https:\/\/api.xively.com\/v2\/feeds\" + \"\/\" + feed_id;\r\nreturn msg;<\/pre>\n<p><strong>AT&amp;T M2X Example<\/strong><\/p>\n<pre class=\"brush:js\">var dev_id = \"&lt;DEV_ID&gt;\";\r\nvar api_key = \"&lt;API_KEY&gt;\";\r\nvar stream_name = \"&lt;STREAM_NAME&gt;\";\r\n\r\nvar dev_url = \"http:\/\/api-m2x.att.com\/v2\/devices\/\";\r\nvar stream = \"\/streams\/\" + stream_name + \"\/value\";\r\n\r\nvar data = {\"value\": 30};\r\n\r\nvar msg = {\r\n\t\"method\" : \"PUT\",\r\n\t\"url\" : dev_url + dev_id + stream,\r\n\t\"headers\" : {\r\n\t\t\"Content-Type\": \"application\/json\",\r\n\t\t\"X-M2X-KEY\": api_key\r\n\t},\r\n\t\"payload\" : JSON.stringify(data)\r\n};\r\n\r\nreturn msg;<\/pre>\n<\/li>\n<li>Double click on the\u00a0<strong>http request<\/strong> node, and change the <strong>Method<\/strong> to <strong>&#8211; set by msg.method &#8211; <a href=\"https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/edit-http-request.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"623\" class=\"aligncenter  wp-image-9501\" alt=\"edit-http-request\" src=\"https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/edit-http-request-1024x623.png\" srcset=\"https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/edit-http-request-1024x623.png 1024w, https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/edit-http-request-300x182.png 300w, https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/edit-http-request.png 1323w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a> <\/strong><\/li>\n<li>Double click on the <strong>debug<\/strong> node and change <strong>Output<\/strong> to <strong>complete msg object<\/strong>.<\/li>\n<li>Press OK and Deploy<\/li>\n<li>Pressing the inject node button will trigger the http request node to fire. You should see something like this:<\/li>\n<\/ol>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/http-request-debug.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"622\" class=\"aligncenter  wp-image-9503\" alt=\"http-request-debug\" src=\"https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/http-request-debug-1024x622.png\" srcset=\"https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/http-request-debug-1024x622.png 1024w, https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/http-request-debug-300x182.png 300w, https:\/\/www.multitech.net\/developer\/wp-content\/uploads\/2015\/10\/http-request-debug.png 1324w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>Troubleshooting<\/h4>\n<ul>\n<li>You get an error with <strong>node-red:httpin.errors.not-overridden message<\/strong> or <a href=\"http:\/\/bit.ly\/nr-override-msg-props\" target=\"_blank\">http:\/\/bit.ly\/nr-override-msg-props<\/a> url.\n<ul>\n<li>Double click on the\u00a0<strong>http request<\/strong> node, and change the <strong>Method<\/strong> to <strong>&#8211; set by msg.method &#8211; <\/strong><\/li>\n<\/ul>\n<\/li>\n<li>Test the API using <code>curl<\/code> from a command line\n<pre class=\"brush:shell\">$curl --request PUT \\\r\n--data '{\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"one\", \"current_value\":\"100.00\"}]}' \\\r\n--header \"X-ApiKey: &lt;API-KEY&gt;\" \\\r\n--verbose \\\r\nNEW_FEED_URL\r\n# ^^^ replace this with new feed's URL<\/pre>\n<\/li>\n<li>Connect the debug node to the function node and verify the msg object properties match the above curl example:\n<pre class=\"brush:js\">--request       -&gt;  msg.method\r\n--data          -&gt;  msg.payload\r\n--header        -&gt;  msg.headers\r\n&lt;last_param&gt;    -&gt;  msg.url<\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>NOTE:\u00a0Support for Node-RED\/Node.js on Multitech AT91SAM9G25-based products has been discontinued within mPower 5.3. For mPower 5.3.3 to 5.3.8, users can\u00a0install Node-RED as a custom application.\u00a0mPower 6.0 or later no longer supports this custom app option.\u00a0 The following instructions are still applicable for mPower MTCDT 5.2.1 and MTCAP 5.2.1 and earlier on all devices. And also [&hellip;]<\/p>\n","protected":false},"author":1337,"featured_media":0,"parent":8240,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"class_list":["post-9498","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/9498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/users\/1337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/comments?post=9498"}],"version-history":[{"count":26,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/9498\/revisions"}],"predecessor-version":[{"id":9538,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/9498\/revisions\/9538"}],"up":[{"embeddable":true,"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/pages\/8240"}],"wp:attachment":[{"href":"https:\/\/www.multitech.net\/developer\/wp-json\/wp\/v2\/media?parent=9498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}