Finding out Product's Collections

Highlighted
New Member
6 0 0

Hi there,

I notice there is an API for us to find out all the products in a specific collection (https://shopify.dev/docs/admin-api/rest/reference/products/collection). However, is there any API for us to reverse finding out what are the collections related to a specific product besides the Collect API (https://shopify.dev/docs/admin-api/rest/reference/products/collect). Collect API is not ideal for us as it does not provide a product_id filter.

Thank you.

0 Likes
Highlighted
Shopify Partner
533 38 109

Using the GraphQL API it's pretty straightforward. Below is an example, where based on a given product ID we pull the first 5 collections its a member of, as well as the first 5 publications (aka sales channels) that each collection is featured on.

Hope this helps!

{
  product(id:"gid://shopify/Product/4423636484148") {
    id
    legacyResourceId
    title
    collections(first: 5) {
      edges {
        node {
          description
          handle
          resourcePublications(first: 5) {
            edges {
              node {
                publication {
                  id                  
                  name
                }
              }
            }
          }
        }
      }
    }
  }
}
0 Likes
Highlighted
New Member
6 0 0

Thanks @Greg_Kujawa for your reply. So I would imagine at the time being, it is not achievable through the REST API endpoint. 

0 Likes
Highlighted
Shopify Partner
533 38 109

I'm unsure if that's the case or not. I started out interfacing with Shopify using the REST API. When I ran into some gotchas or endpoint fields that weren't accessible with it I then looked at the GraphQL API. While the mindset of working with it is a lot different and takes a bit of getting used to, moving forward I am going to exclusively rely on it!

1 Like