Get a list of ALL Products (not just the first 50)

Solved
TxBob77
Excursionist
15 1 2

If I use the Products API it only returns 50. I realize this is a limit, but I new the whole list returned or a way of looping through and get the whole list 50 at a time.  How can this be done?  What is the API call?

0 Likes

You need to use pagination to loop through all of the products and store them to a variable or database.

How you do this will depend on if you are using the rest api or the graphql api (and the development stack you are using):

 

rest pagination:

https://help.shopify.com/en/api/guides/paginated-rest-results

 

graphql pagination:

https://help.shopify.com/en/api/getting-started/shopify-and-graphql/pagination

0 Likes
TxBob77
Excursionist
15 1 2

Do you know if the GraphQL API allows for a single query to return ALL the products?  or is it still throttled by pagination?

0 Likes

The pagination is based off of request tokens, so the answer on this depends on what information you are trying to get and how many products you have.

 

You generally start with 1000 tokens:

https://help.shopify.com/en/api/graphql-admin-api/graphql-admin-api-rate-limits

TxBob77
Excursionist
15 1 2

This is an accepted solution.

I discovered the using the "products.json?since_id=<last id>"  is another way to do this.  First set <last id> to be 0 and then put in a loop such that <last id> is set to the last product id returned, exit the loop when the returned product.count is 0.

meganspauldingc
Tourist
3 0 1

I read the REST documentation but can not for the life of me figure out how to find the HTTP Link value. The response returned, when logged, only gives me the body. console.log(response.headers) returns empty as well. Any insight here on how to actually GRAB the value in order to be able to make the get call as outlined in the documentation?

0 Likes
ScottStevens_GM
Tourist
10 0 3

First, Install the Nuget Package ShopifySharp (if you want) - Makes it alot easier to communicate with Shopify

//Init Shopify Class

private ShopifyApi _api = new ShopifyApi(Settings.Default.ShopifyClientID, Settings.Default.ShopifySecret, Settings.Default.ShopifySellHost);

//Set Models

private List<ShopifyProduct> shopProducts = new List<ShopifyProduct>();

private class ProductResponse
{
public ShopifyProduct[] products { get; set; }
}

//Method to Get Product List

private void GetShopifyProducts()
{

//Loops and Continues until no more returned
long? lastID = 0;
while (lastID >= 0)
{
var jsonString = _api.Get("api/" + Settings.Default.ShopifyAPIVersion + "/products.json?since_id=" + lastID.ToString());
var myDTO = JsonConvert.DeserializeObject<ProductResponse>(jsonString);
if (myDTO != null && myDTO.products.Count() > 0)
{
shopProducts.AddRange(myDTO.products.ToList());
lastID = myDTO.products.Last().Id;
}
else
{
lastID = -1;
break;
}
}
}