Best way to query for a release by a tag value in v5 API?

I am using the V5 API and I want to be able to get releases (really just 1) by a value in a tag attached to it. Looking over the API docs I’m not sure there is a straightforward way of doing this. Any ideas?

Hi @jchoate2 - welcome to the forums!

The following javascript snippet demonstrates how to retrieve a release with a tag of foo=bar. Additionally if you only want one you can add &$top=1 to the query:

const fetch = require('node-fetch')

const JWT_TOKEN = "YOUR_TOKEN";

const APP_ID = 1234;

const main = async () => {
    const resourceURL = "https://api.balena-cloud.com/v5/release";
    const filter = `belongs_to__application eq ${APP_ID} and release_tag/any(t:t/tag_key eq 'foo' and t/value eq 'bar')`

    const response = await fetch(`${resourceURL}?$filter=${filter}`, {
        headers: {
            "Authorization": `Bearer ${JWT_TOKEN}`,
        }
    });

    console.log(await response.json())
}

main()

Hope that helps!

Cheers,
James.

This code was working fine in December but now it returns no results. It seems that the tag portion of the filter is not working. Has the API changed?

Hi, I was able to successfully perform this type of query using curl. Do you get any specific errors when performing the request? Could you try the same from the command line?

export API_TOKEN="YOUR TOKEN"
export APP_ID=1234
curl -X GET --header "Authorization: Bearer ${API_TOKEN}" "https://api.balena-cloud.com/v5/release?\$filter=belongs_to__application%20eq%${APP_ID)%20and%20release_tag/any(t:t/tag_key%20eq%20'foo'%20and%20t/value%20eq%20'bar')"

Thanks!

When I run it from the command line, the command succeeds and returns no results. (d[]) which is the same as from the code. It never returns an error even with nonsense tags that don’t exist. This leads me to believe that the tag query is doing something differently, failing to find a match and returning an empty result vs. encountering an error. If the filter only includes the $filter=belongs_to__application%20eq%${APP_ID) then I successfully get a list of all of the releases. My use case is that for our automation I want to find the hash for specific builds based on our version number that we set as a tag: “ver: 123”. The last time I needed to run this automation was in December and it ran without an issue, but when I ran the same code the other day it failed to find the build.

Hi @jchoate2, I’m not sure why the original query stopped working honestly. Then I believe there is a typo in the query pipex shared. Can you please try the following?

export API_TOKEN="YOUR TOKEN"
export APP_ID=1234
export TAG_KEY=ver
export TAG_VALUE=123

curl -X GET \
"https://api.balena-cloud.com/v6/release?\$select=commit&\$filter=(belongs_to__application%20eq%20${APP_ID})%20and%20(release_tag/any(rt:(tag_key%20eq%20%27${TAG_KEY}%27)%20and%20(value%20eq%20%27${TAG_VALUE}%27)))" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${API_TOKEN}"

I tested this one I copy pasted and it works fine on my end. Please let us know how it goes for you and if we can help translating this query into SDK version.

Also good to note that the query I shared uses v6 endpoint – which is the latest API version nowadays. In general, we encourage our users to use the latest API version.

Yes! This works perfectly @gelbal! Thank you!