Forum Discussion

JSmith's avatar
5 years ago

API Filtering

Is it possible to apply multiple filters to an API query? If so, would anyone have an example of what it would look like? 

 

Here are my current attempts: 

Working: 

https://agio.logicmonitor.com/santaba/rest/device/devices?filter=deviceType:0

 

Not working: 

https://agio.logicmonitor.com/santaba/rest/device/devices?filter=deviceType:0,name-QA*

 

Thanks! 

  • Anonymous's avatar
    Anonymous

    As far as i can tell, even in the examples, these filters aren't looking for a single property with the name and value. it's looking a device having a property with name sn.sys_class_name and also having a property with value cmdb_ci_hardware. However, i don't think there's anything in the filter logic that actually pairs the two up, looking only for a property with name sn.sys_class_name also having the value cmdb_ci_hardware. Also, looks like the criteria also has to be in quotes. Not sure if this is exactly how it works under the hood, but this is how I understand it: the api looks at the JSON and evaluates it against the filter. There's an object in the JSON for each returned device called "customProperties". That object is a list, so it contains objects, each of which has a name and a value. It would appear that if you specify a dot in the field name (i.e. customProperties.name) the filter uses an "in" operator to see if your criteria (sn.sys_class_name) is in the list somewhere. In this case, it's looking to see if there's a property with name = "sn.sys_class_name" anywhere in the customProperties object. It would appear to repeat this process for the value. 

    That said, I personally don't do much filtering in my API call beyond a single criterion. My API calls are already being called from a script which gives me the opportunity to do post request-response processing, so I tend to pull down more than I usually need and do further filtering in the script. Not making excuses, just offering how I work around the issue.

  • Anonymous's avatar
    Anonymous

    This is working for me using both v1 and v2 of the api:

    {{url}}/device/devices/?filter=deviceType:0,id:9

    It looks like your name filter isn't constructed properly maybe? It needs a ~ instead of a dash. Try them both individually to make sure they both work by themselves.

  • Anonymous's avatar
    Anonymous

    Did some playing around and it looks like name and displayName both need to encapsulate the criteria in quotes. I couldn't get the name filter to work with another filter, but this is what it looks like by itself.

    @Sarah Terry, any idea how to combine the name filter with another filter? Whenever i combine it the API thinks it's an invalid filter (error 1400).

  • Hey Stuart, appreciate the quick responses! 

    I really don't need the name filter, it was just one I seen in the API docs and was using it as a reference point. I thought the issue was just combining filters, it appears though that it was my query. Here is one that just worked: 

    https://site.logicmonitor.com/santaba/rest/device/devices?filter=deviceType:0,disableAlerting:true

     

    To take this one step further, would the filter be different for a custom property? These examples are giving me a hard time trying to convert for my use case. Albeit, I am using v2 of the API but, I cannot find any difference in how the filtering would work. 

    https://site.logicmonitor.com/santaba/rest/device/devices?filter=deviceType:0,customProperties.name:sn.sys_class_name,customProperties.value:cmdb_ci_hardware