Coding an UbiFunction

UbiFunctions accept NodeJS or Python, which means you can use Javascript or Python code to extract, transform, and analyze data.

Before coding a function, you'll need to get familiar with these 3 concepts:

  • Input arguments

  • Code

  • Output response

Input Arguments

Any payload sent to the function will be accesible in the "args" object inside the code.

Object

Description

args

Any payload sent to the function will be contained by the args object.

args.token

When an x-auth-token header is included in an HTTP invokation request, the args.token key will be included.

Code

Your UbiFunction code is a NodeJS or Python snippet that runs when the function is invoked.

As an example, the following code would return the same data received:

function main(args) {

    return args;

}

By default, every new UbiFunction has a sample code that uses input data (token, device, and a variable value) to make a request to Ubidots API. Here are the default examples for your reference:

const axios = require('axios');

// Main function - runs every time the function is executed.
// "args" is a dictionary containing both the URL params and the HTTP body (for POST requests).
async function main(args) {

  // Grab the token and device label from URL parameters, then erase them from the args dictionary
  var ubidots_token = args.token;
  var device_label = args.device;
  delete args['token'];
  delete args['device'];

  // Use the remaining parameters as payload
  var payload = args;

  // Log the payload to the console, for debugging purposes. You may access the function's logs using
  // the option in the above header.
  console.log(payload);

  // Send the payload to Ubidots
  var response = await ubidots_request(ubidots_token, device_label, payload);

  // Log Ubidots response to the console
  console.log(response);

  // Pass Ubidots' API response as the function's reponse
  return response;
}

// This function builds an HTTP POST request to Ubidots
async function ubidots_request(token, label, body) {
  let config = {
    method: 'post',
    url: 'https://industrial.api.ubidots.com/api/v1.6/devices/' + label,
    data: body,
    headers: {
      'Content-Type': 'application/json',
      'X-Auth-Token': token
    }
  }
  const response = await axios.request(config);
  return response.data;
}

To test the function you can click on the button Run Function. This will open up a modal requesting the input data to test with, you need only to enter a valid JSON payload. You may leave this blank if your code does not require any input.

In the case of Ubidots default sample function, enter the JSON payload below to run a test:

{
    "token":"your-token",
    "Device":"sample-function",
    "Temperature":"45"
}

After clicking on Run with this input a console window will emerge with both the output and the console of the results:

Output Response

The output of every UbiFunction must be a JSON object. By default, the status code will be 200, but this could be changed using Raw Functions.

For example:

function main(args) {

    return {"Hello":"World"};

}

Last updated