Balena API calls fail with "Database Error"

Good evening,

I am trying to automate some stuff with the balena API (v4).
So far every call worked, but I am running into problems with device related GET calls.

While I can set device config, environment and service variables, I always get a Database Error when trying to get the variables with e.g. the following command:

curl -X GET \
https://api.balena-cloud.com/v4/device_config_variable?\\$filter=device%20eq%20$deviceUUID” \
-H “Content-Type: application/json” \
-H “Authorization: Bearer $apiKey”

Is this a temporary API failure (can’t seem to find any malfunction on your status page) or is it a more permanent thing?

Greetings,
Tarek

Hey @Tschebbischeff ,

I have just tested on my end and it worked.
Could you test using this URL and let me know if it works:

https://api.balena-cloud.com/v4/device_environment_variable?%5C%24filter=device%20eq%20<UUID>

Cheers

Hm, that one worked, so I gotta URL encode the “\$”? It works without enconding on the other endpoints :open_mouth:
Thanks for the quick fix! :slight_smile:
You might want to request someone to change the documentation if its intended to be encoded though :stuck_out_tongue:

Greetings and Thanks,
Tarek

@Tschebbischeff

I am glad it worked. I will ping the team about this.

All the best

1 Like

As a quick follow-up, it seems that replacing \$ with the encoded version in other queries will yield weird results. As it seems for example filters will not work, so this encoding behavior seems specific to only some of the requests.

Thank you for the additional info :slight_smile:

@Tschebbischeff

Just one more clarification, the \ is used there just so that bash doesn’t think that $filter is an environment variable.

And a second one that I noticed is that the docs say

curl -X GET \
"https://api.balena-cloud.com/v4/device_config_variable?\$filter=device%20eq%20<DEVICE ID>" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTH_TOKEN>" 

<device-id> instead of <uuid>, and this will probably yield in errors.

There are two things you can do

  • Change the filter function to $filter=device/any(d:d/uuid eq $UUID)
  • You can get the device id from a query like this, for example:
curl -X GET \
"https://api.balena-cloud.com/v4/device?\$filter=uuid%20eq%20'<UUID>'" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTH_TOKEN>"

Cheers

1 Like

Alright, that explains a lot actually. Thanks for getting back to me.
So my original error was just the fairly stupid assumption of the device ID being the device’s UUID.

The error with the encoded version of the URL is then probably that the backslash was encoded as well, maybe escaping the dollar sign at some deeper level instead of just in bash, hence making the filter function not work properly.

Thanks for all the quick help and have a great day!
Tarek

Glad it helped, thanks so much !