Ubidots Community

Is there a more efficient way to get data from API

I am fairly new to the Ubidots API…

I need to get 3 variable values from each device into client side code. I have a cloud function that the code hits and returns all the devices that I then parse out the variable URL’s from and hit the API again requesting each variables value. I have about 300 devices and this equals quite a few API requests. Is there a way to do this with one HTTP request?

Im not quite sure if you already mean what I am suggesting when you refer to ‘cloud function’ but I have a similar requirement whereby I need data from multiple devices delivered via one HTTP request and solved it by using the Ubidots functions to create a single endpoint to request from and that function did all the hard work of requesting data from the individual devices, packaging it all up into a single JSON object and returning that.

Thanks @mantracourt I’ll give that a try. I’m trying to build a dynamic feature layer for mapbox to ingest. Trying to minimize my requests in doing this as well.

I do something similar. I get a list of devices filtered by organisation and type, put the device ids into a comma-separated string, and then call a URL like:

https://industrial.api.ubidots.com/api/v2.0/variables/?label=depth&device__id__in=61...b5,61...5a&fields=device,lastValue,unit

I’m asking for the variable’s device field so I can reference each variable value back to the device it came from in the list of devices from my initial org/type query.

You could probably get more than one variable using something like label__in=varlabel1,varlabel2

This only gets the most recent value, you need to use the 1.6 data api calls to get older values.

The filtering options on this API are powerful but confusing until you’ve tried them out with CURL etc to see what is accepted.

Did you do this because you really had to get all the info back in one call, or was it quicker making the API calls from the ubifunction than from the browser?

We’re seeing delays of up to 30 seconds making 1.6 API calls for variable values from the browser for HTML canvas widgets.

It was really to simplify the call. All the info from a device’s variables and some decoded info held in the device properties was wrapped up in a single JSON object returned by the single call to the Ubidots function.
Looking at it now this was a single device and do not have speed issues but getting info from the function should not be slower than if you asked for data from each device from a browser.
There is a double hop so that adds an overall latency but that would get less noticeable the more devices you got data from in the function.

Hello to everyone.

The most optimal way of retrieving the variable data for multiple variables I believe is the Data Raw Series endpoint. From this endpoint you can specify a list of variable IDs, a start and end timestamp top get the values from, and other special information that might be of interest (view the API Docs for better reference).

Note: if start and end timestamps aren’t provided, the request will return the variable’s last 100 values. To limit the amount of values retrieved for each variable, use the "limit" key parameter within the body of the request. For example, to get the last 5 values of each variable, then the body would be:

{
"variables": ["624f0ae57cf9a10316a12e7f", ..... , "624jd8727cf9a542316a124gs"],
"join_dataframes": false,
"columns": ["value.value"],
"limit": 4
}

In this case, the better question would be how to obtain the Variable ID list, and that would greatly depend on the specific case:

  • Do you want to obtain data for each variable across all/ a list of devices?
  • Do you want to obtain data for a specific variable across all/ a list of devices?
  • Do you wan tto obtain data for multiple variables across all/ a list of devices?

The filters that @david.a.taylor shared would be great in case you’d like to get the values for a specific variable across a list of Devices.

Best regards,
Sebastián