Connect Node-RED to Any Cloud Service’s REST API
NOTE: 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 install Node-RED as a custom application. mPower 6.0 or later no longer supports this custom app option.
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.
Refer to this alterative example using Python of a default application and server with API implementation for a distributed LoRaWAN network: https://github.com/MultiTechSystems/lorawan-app-connect.
For more details on other methods and examples to create custom applications, see Creating a Custom Application.
Using the function and http request nodes the Conduit® can be configured to communicate with any REST API cloud service.
- Drag the following nodes onto the sheet and connect them sequentially: inject, function, http request, and debug
- Double click on the function node and enter the following example code with settings from your cloud service’s documentation. Xively Example
var apikey = "<YOUR-API-KEY>"; var feed_id = "<YOUR-FEED-ID>"; var data = {}; data["version"] = "1.0.0"; data["datastreams"] = []; data["datastreams"].push({"id":"one", "current_value":"100.00"}); msg.method = "PUT"; msg.headers = { "X-ApiKey": apikey }; msg.payload = JSON.stringify(data); msg.url = "https://api.xively.com/v2/feeds" + "/" + feed_id; return msg;
AT&T M2X Example
var dev_id = "<DEV_ID>"; var api_key = "<API_KEY>"; var stream_name = "<STREAM_NAME>"; var dev_url = "http://api-m2x.att.com/v2/devices/"; var stream = "/streams/" + stream_name + "/value"; var data = {"value": 30}; var msg = { "method" : "PUT", "url" : dev_url + dev_id + stream, "headers" : { "Content-Type": "application/json", "X-M2X-KEY": api_key }, "payload" : JSON.stringify(data) }; return msg;
- Double click on the http request node, and change the Method to – set by msg.method –
- Double click on the debug node and change Output to complete msg object.
- Press OK and Deploy
- Pressing the inject node button will trigger the http request node to fire. You should see something like this:
Troubleshooting
- You get an error with node-red:httpin.errors.not-overridden message or http://bit.ly/nr-override-msg-props url.
- Double click on the http request node, and change the Method to – set by msg.method –
- Test the API using
curl
from a command line$curl --request PUT \ --data '{"version":"1.0.0","datastreams":[{"id":"one", "current_value":"100.00"}]}' \ --header "X-ApiKey: <API-KEY>" \ --verbose \ NEW_FEED_URL # ^^^ replace this with new feed's URL
- Connect the debug node to the function node and verify the msg object properties match the above curl example:
--request -> msg.method --data -> msg.payload --header -> msg.headers <last_param> -> msg.url