Hey everyone! I’m building a Shopify app and I’m stuck on something. I need to fetch all collections that contain a particular product by using the product’s ID.
Let’s say I have a product called “Blue Hoodie” and it’s part of several collections like “Winter Wear”, “Sale Items”, and “Featured Products”. I want to get a list of all these collections programmatically.
Can this be done with the Shopify Storefront API? What would be the right GraphQL query or REST endpoint to use? I’m looking for the correct way to structure this request and wondering if there are any gotchas I should know about.
Thanks in advance for any code examples or guidance!
Been working with Storefront API for two years and hit this exact issue multiple times. The GraphQL approach is solid, but add error handling around product lookups - invalid product IDs will silently kill your entire query. Here’s what caught me off guard: smart collections don’t always return consistent results right after product updates. Shopify processes collection rules in the background, so there’s a delay. Manual collections are instant, smart ones aren’t. If this is customer-facing, build in a fallback or retry mechanism. Also, collection ordering in responses follows Shopify’s internal logic, not what you see in admin.
hit this yesterday too! quick tip - check your app permissions for collection access if you’re using the storefront API. spent way too long wondering why collections weren’t showing up. also watch out for product ID format - you need the base64 encoded gid, not just the plain number.
Had this exact problem a few months ago on a similar project. The Storefront API handles it well with GraphQL, but you need the global product ID - not the regular numeric one from admin. Alice’s query structure works, but add pagination since collections can get huge. Also watch the rate limits - if you’re hitting multiple products at once, batch your requests or add delays between calls. One thing that tripped me up: private collections won’t appear in results. Only published ones that customers can see show up. Double-check your collections are available on your sales channel if they’re not appearing.
You can also tackle this from the collections side if you’re hitting performance issues. Instead of querying each product separately, fetch all collections first and filter by checking which ones contain your target product. This works better when you’re dealing with multiple products at once. You’ll pull more data upfront but skip the repeated API calls. Also consider caching - collections don’t change as much as inventory, so storing that membership data locally can cut down API usage big time. Just don’t forget to clear your cache when collections get updated via webhooks or periodic refreshes.
yep, that’s the way to do it! just make sure to query the product to get the collections. somethin like this should work: query { product(id: "your-product-id") { collections(first: 10) { edges { node { id title handle } } } } }. it helped me out on mine!