REST Adming API Get Products By Ids by call query paramter 'limit' not working properly

Solved

REST Adming API Get Products By Ids by call query paramter 'limit' not working properly

DavisPresley
Shopify Partner
6 1 2

Hi, I am developing a shopify app and I want get store products by ids using this api call - (https://shopify.dev/docs/api/admin-rest/2024-01/resources/product#get-products?ids=632910392,9217287...).
The issue is as follows when I make the request with query parameter limit set to 250 I get only 50 products returned as a response. The thing is I can see that the limit in response is correctly set to 250, however I am only getting 50 products. Has someone ran into this issue before?

Accepted Solution (1)
DavisPresley
Shopify Partner
6 1 2

This is an accepted solution.

Thanks, I am doing it that way of course, I think I found the solution you just have to specify the limit parameter in path before the product ids you want to retrieve like this:

      const data = await client.get({ path: `products.json?limit=${CHUNK_SIZE}&ids=${chunk.join(',')}` });

View solution in original post

Replies 5 (5)

SomeUsernameHe
Shopify Partner
495 54 99

Can I see the code you are using to make the request? 

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
DavisPresley
Shopify Partner
6 1 2

/* CLIENT PROVIDER */
const
restClient = async ({ req, res, isOnline }) => {
  const session = await fetchSession({ req, res, isOnline });
  const client = new shopify.clients.Rest({ session });
  const { shop } = session;
  return { client, shop, session };
};

const clientProvider = { graphqlClient, restClient };
export default clientProvider;
   

// rest client
    const { client } = await clientProvider.restClient({
      req,
      res,
      isOnline: false,
    });

/* REQUEST */
        let requestBody = {
          path: `products.json?ids=${chunk.join(',')}`,
          query: { limit: 250 }
        }

        let data = await client.get(requestBody);

chunk is an array of product ids I confirmed the length of the chunk is 250 as it should be 
SomeUsernameHe
Shopify Partner
495 54 99

I would try and add a little bit of error handling for this issue:

/* CLIENT PROVIDER */
// ... existing code ...

/* REQUEST */
try {
  let requestBody = {
    path: `products.json?ids=${chunk.join(',')}`,
    query: { limit: 250 }
  }

  let response = await client.get(requestBody);
  if (response.headers && response.headers.link) {
    console.log('More products available for pagination');
  }
  let data = response.body;
  // Additional processing of data
} catch (error) {
  console.error('Error fetching products:', error);
  // Handle the error appropriately
}

 

Shopify API uses pagination for large sets of data. If the response includes a link header, it indicates that there is more data available, and you will need to make additional requests to retrieve the remaining data.

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
DavisPresley
Shopify Partner
6 1 2

This is an accepted solution.

Thanks, I am doing it that way of course, I think I found the solution you just have to specify the limit parameter in path before the product ids you want to retrieve like this:

      const data = await client.get({ path: `products.json?limit=${CHUNK_SIZE}&ids=${chunk.join(',')}` });

SomeUsernameHe
Shopify Partner
495 54 99

I would post this on the Shopify GitHub under issues and see if this might be a bug?

 

Just from my experience, this shouldn't normally be a thing. You should be able to still use limit if you specify it after the ids.

 

I don't see anything in the documentation that states that. But I could be totally wrong!

 

Glad you got it fixed. 

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee