Ubidots Community

Filter values from variable API Request

Hi Guys I’m using a HTML Canvas to create somes Charts with JS, I use Variables API REST and Values, Actually I get all values from specific variables using https://industrial.api.ubidots.com/api/v1.6/variables/${inVariableKey}/values/?page=1 before i filter the Array result with .filter taking as reference value timestamp but I think that it is not the better.

I would like filter te api result like https://industrial.api.ubidots.com/api/v1.6/variables/${inVariableKey}/values/timestamp__day__range=2022-04-03,2022-04-04

I am having as reference Variable Filters

ubidots.on('ready', function (data) {
    const token = '';
    const variableId = '';
    getValuesFromVariable(variableId , token)

        .then((dataResult) => {

            console.log("getValuesFromVariable dataResult", dataResult);

        })

        .catch((error) => {

            console.error("getValuesFromVariable error", error);

        });

});

async function getValuesFromVariable(inVariableKey, inToken) {

    const url = `https://industrial.api.ubidots.com/api/v1.6/variables/${inVariableKey}/values/`;
    const response = await fetch(url, {

        method: 'GET',

        headers: {

        'Content-Type': 'application/json',

        'X-Auth-Token': inToken,

        },

    });

    return response.json();

};

Hi @cristiankmb,

Before explaining what you need to do in order to filter the values of a variable based on time range through the API, allow me to first make a clarification: the Variable filters you mentioned isn’t for the variable data but for the Variable entity itself as stored in Ubidots. The API v2.0 (the one you referenced) scope is only CRUD (Create, Read, Update and Delete) operations over the different available entities, namely, Devices, Variables, Organizations, among others found at the HTTP RESTful v2.0 API, but it doesn’t scope data values.
Instead, for Data operations (send or read) you should refer to the Data API, where each supported protocol is explained in its own section.

Now, to your case, you should refer directly to the v1.6 /variables endpoint, specifically to the query params and examples sections. There, you’ll find how to filter the data based on a time range directly from the endpoint. In a brief, your URL should be something like the below:

https://industrial.api.ubidots.com/api/v1.6/variables/${inVariableId}/values/?start=${timestampStart}&end=${timestampEnd}

where timestampStart and timestampEnd correspond to the time range you want to filter the data based on in POSIX milliseconds format.

I hope this makes sense.