Losant API Node Configuration

Hi,

I’m trying to use the Losant API node to configure the Get: Devices service within an experience workflow. I configured all the parameters that are passed as a query, but when a field is not passed, it is not disregarded, it seems to me that it continues to be sent (perhaps as an empty string), as can be seen in the API validation. Is there any other way to configure this node to disregard the field, when no value is passed?

The workflow is just this.

image

@Vander_Maziero,

We just released a new template helper I think would be helpful here for your problem:

{{defaultTo val default}} : Returns the value of default if the value of val is null , undefined , or an empty string. Otherwise, returns the value of val (from the Documentation.

In this case, you could do something like:

{{defaultTo data.query.sortField 'name'}}

Since the sortField default is ‘name’, it would default to the API’s default or use the value at the given payload path if you provided no value. And you can take this approach for each parameter of the API; the default you set is the API’s default.

That should give you the behavior you desire.

1 Like

Thanks @anaptfox , this is useful for some query parameters, but for “filterField” I tried using {{defaultTo data.query.filterField ‘’}} or {{defaultTo data.query.filterField null}}, but it didn’t work as “Blank or not provided means no filtering”. Do you have any other suggestions?

Hey @Vander_Maziero,

Are you hoping to do some filtering on the devices you query or would you like not to have any filtering applied?

When you use {{defaultTo data.query.filterField ‘’}} or {{defaultTo data.query.filterField null}} does the API response show an error similar to the one you first posted?

Thank you,
Heath

Hey @Heath,

This, I would like to perform the filter if the “filterField” property was passed, according to the documentation “Blank or not provided means no filtering”, if it were not passed, the filter would not be performed. However when using {{defaultTo data.query.filterField ‘’}} the API does not seem to identify as “Blank or not provided” and responds as in the first case.

When I pass a value to the “filterField” field

When I don’t pass a value to the “filterField” field does the API response show an error similar to the one first posted

@Vander_Maziero,

I looked into this, and have a solution for you.

filterField, when nothing is sent to it, defaults to sorting by name. When you send something in the filterField field, it only accepts name as an input.

The solution, then, is to either leave this field blank in the API Node, or only send name.

Thank you,
Heath

@Heath,

Yes, this is exactly what I’m trying to do dynamically (leaving the generic configuration, as in the documentation), but even if I use {{data.query.filterFiled}} and that doesn’t exist in the payload, the node doesn’t consider it as a blank field, does the API response show an error similar to the one first posted. If I have to leave the field blank, and I can’t use a string template, it doesn’t become dynamic.

@Vander_Maziero,

@Heath’s recommendation in template form would be:

{{defaultTo data.query.filterField "name" }}

The only accept entry for that field is “name”. So, the above template is the same as leaving the field blank.

Okay, but it doesn’t work as expected for all fields. If I use the "parentId" field like {{data.query.parentId}}, {{defaultTo data.query.parentId null}} or {{defaultTo data.query.parentId undefined}} everyone makes the request as “parentId” : “”, it is not possible to disregard this value, the only possibility is passing the empty field, which is not dynamic.

{{data.query.parentId}}, where data.query.parentId is undefined, is considered as an empty string:

passing the empty field

So what I meant is that even a value that is undefined is being passed to the request, in this case as an empty string. I think that if {{data.query.parentId}} does not exist, the node should not pass any value on the request, correct?

I’m trying to replicate APIs for experience users, so that experience users can directly consume the APIs without having to create API Tokens. So I would just manipulate the query, adding

{
    "experienceUserId": {
        "$eq": {{experience.user.id}}
    }
}

and the other fields would be the same as the documentation

@Vander_Maziero,

That is correct. My comment only applied to the “filter” field. Unfortunately, you can’t default to null or undefined using templating. Templates will always resolve to a string ( even an empty one ).

In this case, you’d have to perform the logic in the Conditional Node, and branch the workflow based on the presence of the fields you want to optionally provide. ( for more complex cases, the Switch Node could be useful too ).

I am asking the engineering team to see if they have any other options / better workarounds. But, for now, branching seems like the best path forward.

1 Like

Thanks @anaptfox,

A suggestion would be the possibility of passing the parameters of Devices: Get as Payload Path, instead of individual fields, as well as for other services, such as Data: Time Series Query. So if there was no value for the parameter, it would not be passed in the query.

image