Connect Node-RED to Any Cloud Service’s REST API

Using the function and http request nodes the Conduit can be configured to communicate with any REST API cloud service.

  1. Drag the following nodes onto the sheet and connect them sequentially: inject, function, http request, and debug
  2. 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;
  3. Double click on theĀ http request node, and change the Method to - set by msg.method – edit-http-request
  4. Double click on the debug node and change Output to complete msg object.
  5. Press OK and Deploy
  6. Pressing the inject node button will trigger the http request node to fire. You should see something like this:

http-request-debug

 

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