All Collections
Developer Guides
Use UbiFunction to ingest sensor data using HTTP GET requests
Use UbiFunction to ingest sensor data using HTTP GET requests

Learn how to create a custom API that accepts HTTP GET requests and forwards the sensor data to Ubidots' API

S
Written by Sergio M
Updated over a week ago

In this example you will learn how to:  

  • Use URL arguments of an HTTP GET request inside a Function

  • Make an HTTP POST request from a Function

At the end of this example you should be able to paste a URL with this format into your browser to send data to your devices:

https://parse.ubidots.com/prv/YOUR-USERNAME/post-data?&device_label=ABCD&temperature=73&humidity=55&token=YOUR-ACCOUNT-TOKEN

Creating a Function

To create a function you need to have a Professional Ubidots subscription. Go to the "Devices" tab and select "Functions":

Next, create a new Function and give it a descriptive name that resembles the parse function you will be executing; in this case we called it "Post data". In the HTTPS Endpoint URL, select "GET" as the method to trigger the function:

Coding your Function

The following code has two functions: 

  • A main function than accepts "args" as an input. This input is a dictionary containing the URL parameters as "key":"value" pairs. For example, if we triggered the function with this URL:

https://parse.ubidots.com/prv/YOUR-USERNAME/parser-name?hello=world&param1=message1&param2=message2

Then the main function would receive them like this:

args = {"hello":"world", "param1":"message1", "param2":"message2"}
  • A ubidotsPost function, which uses the specified token, device_label and a JSON payload as an input, in order to make an HTTP POST request to Ubidots' API.

By using these two functions, we obtain the ability to (1) map the URL parameters to individual variables inside the function (token, device_label, and a JSON payload containing the variables and values to be sent), and (2) run the ubidotsPost function to send the obtained data to Ubidots:


var request = require('request-promise');

async function main(args) {

    // Get the device label from the request URL, then erase it from the args payload

    var device_label = args.device_label;

    delete args['device_label'];

    // Get the token from the request URL, then erase it from the args payload
    var token = args.token;

    delete args['token'];
   
    // Make POST request to Ubidots
    var post_response = await ubidotsPost(token, device_label, args);

    // Pass Ubidots' API response to the function' response

    return post_response;

}

async function ubidotsPost(token, device_label, data) {

    var options = {

        method: 'POST',

        url: 'https://industrial.api.ubidots.com/api/v1.6/devices/' + device_label + '?force=true',

        body: data,
        json: true,
        headers: {
            'Content-Type': 'application/json',
            'X-Auth-Token': token
        }
    };
    return await request.post(options);
}

The result

If we paste the URL below into our browser, we can obtain the {"status_code": "201"} responses from Ubidots' API stating the data was posted successfully!

And then of course, if we go to our Ubidots account, we see a device with label "abdc" and the respective variables and values being updated at the intervals your function is scheduled to run.

Want to share a helpful function to teach others how to connect the dots? Feel free to post it at community.ubidots.com and help another IoT innovator achieve their goals. 

Did this answer your question?