Count number of devices trough the API

Hi!

I like to count the number of devices based upon a filter via the API. Getting a list of devices is done like and works fine:

https://api.balena-cloud.com/v5/device?%24filter=is_online%20eq%20true&%24filter=is_on__commit%20eq%20'65dbac475a47007534da4774f367abb876f7819e'

According to the odata documentation, this is a possibility to do a count of items, with count or $count. I see the dashboard uses this too. However, I don’t have any luck en just get normal results.

https://api.balena-cloud.com/v5/device?%24count=true&%24filter=is_online%20eq%20true&%24filter=is_on__commit%20eq%20'65dbac475a47007534da4774f367abb876f7819e'

Is this possible with Balena API?

Hi, you can try this format to get the count:

https://api.balena-cloud.com/v5/device/$count?$filter=is_online%20eq%20true&$filter=is_on__commit%20eq%20%27bc1294cc67486ea2ca80b07353f96bba%27'

Here’s the unencoded query for easier reading:

https://api.balena-cloud.com/v5/device/$count?$filter=is_online eq true&$filter=is_on__commit eq 'bc1294cc67486ea2ca80b07353f96bba'

With https://api.balena-cloud.com/v5/device/$count I get a 400

"Malformed url: '/resin/device/%24count?%24filter=is_online%20eq%20true&%24filter=is_on__commit%20eq%20'65dbac475a47007534da4774f367abb876f7819e''"

Hey, I think there was an extra ' at the end of the query my colleague shared. It should read:

https://api.balena-cloud.com/v5/device/$count?$filter=is_online%20eq%20true&$filter=is_on__commit%20eq%20%27bc1294cc67486ea2ca80b07353f96bba%27

My wish still to make this call. Only just came past the malformed URL issue. Seems Insomnia API program doesn’t do a well job with the \ and $

curl -X GET "https://api.balena-cloud.com/v6/device/\$count?\$filter=belongs_to__application%20eq%<APPID>&\$filter=is_online%20eq%20true" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>"

So Insomnia is not the only one with this problem. Our system to make \ to %5C as is normal for a URL. However, when I try with curl too %5C$ does not work while \$ does. And with some systems, our too, you cannot use \ without converting it to `%5C’. How do we come over this issue?

Those \$ are needed by the shell because $ is interpreted as string interpolation. \ are not part of the URL but $ are. The “real” URL in your previous example is:

https://api.balena-cloud.com/v6/device/$count?$filter=belongs_to__application%20eq%<APPID>&$filter=is_online%20eq%20true

So when you curl this URL with \ replaced by %5C, the shell tries to expand $count and $filter to their values, which are most likely empty.

By using single quotes (') instead of double quotes ("), $ doesn’t need to be escaped:

curl -X GET 'https://api.balena-cloud.com/v6/device/$count?$filter=belongs_to__application%20eq%<APPID>&$filter=is_online%20eq%20true' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>"