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

Solved
TxBob77
Excursionist
15 1 4

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?

Accepted Solution (1)

Accepted Solutions
TxBob77
Excursionist
15 1 4

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.

View solution in original post

Replies 15 (15)
AddisonApps
Shopify Expert
55 6 12

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

TxBob77
Excursionist
15 1 4

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

AddisonApps
Shopify Expert
55 6 12

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 4

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?

ScottStevens_GM
Tourist
11 0 5

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;
}
}
}

Ufi
Shopify Partner
10 0 0

This is my solution using C# and ShopifySharp 

public async Task<IEnumerable<Product>> ProductsGetList(string shopUrl, string accessToken)
{
    var products = new List<Product>();
    var service = new ProductService(shopUrl, accessToken);

    long? lastId = 0;
    while (lastId >= 0)
    {
        var filter = new ProductListFilter
        {           
            SinceId = lastId            
        };        

        var productList = await service.ListAsync(filter);
        if (productList != null && productList.Items.Any())
        {
            products.AddRange(productList.Items);
            lastId = productList.Items.Last().Id;
        }
        else
        {
            break;
        }
    }

    return products;
}

 

 

MajoMFR
Tourist
11 0 0

I try to do the same but it return 404 because the ? in url is converted to %3F
how  to stop it from converting 

Ufi
Shopify Partner
10 0 0

I when with GraphQL solution.
You need this package:
https://github.com/graphql-dotnet/graphql-client


This is my method:

 

public async Task<List<Product>> ProductsGetListByGraphQL(string shopUrl, string accessToken, string search)
        {
            var client = new GraphQLHttpClient(GraphQLURI.Replace("myshopifydomain", shopUrl), new NewtonsoftJsonSerializer());
            client.HttpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", accessToken);
            var query = new GraphQLRequest
            {
                Query = $@"
                    {{
                        products(first: 50, query: ""title:*{search}*"") {{
                            edges {{
                                node {{
                                    id
                                    title
                                    handle
                                }}
                            }}
                        }}
                    }}"
            };

            var products = new List<Product>();
            var response = await client.SendQueryAsync<ProductResponse>(query);
            if (response.Data != null)
            {

                foreach (var item in response.Data.Products.Edges)
                {
                    products.Add(new Product
                    {
                        Id = long.Parse(item.Node.Id.Replace("gid://shopify/Product/", string.Empty)),
                        Title = item.Node.Title,
                        Handle = item.Node.Handle
                    });
                }
            }
            return products;

        }

 

 

tewe
Shopify Partner
234 44 100

Hi @TxBob77 ,

some additional hints in respect to the REST API:

  • There is a paramter limit which is defaulted to 50. You can set it up to 250, see documentation
  • If you make a get request you also receive header links in the response header which you can use to retrieve the next set of products. These header links look somehow like below

     

 

{'next': {'url': 'https://plattenladen1.myshopify.com/admin/api/2020-
10/products.json?
limit=3&fields=id%2Ctitle&page_info=eyJsYXN0X2lkIjo1OTk5NzU1OTE5NTI2LCJsYXN
0X3ZhbHVlIjoiQWRhbSBBbmQgVGhlIEFudHMgLSBEaXJrIFdlYXJzIFdoaXRlIFNveCIsImRpcm
VjdGlvbiI6Im5leHQifQ',
'rel': 'next'}}​

 

Hope that this helps somebody

Regards

Thomas

• Was my reply helpful? Click Like to let me know!
• Was your question answered? Mark it as an Accepted Solution
• Check out our Price Updater App
paur94
New Member
3 0 0

First of all go to "https://your-store/admin/api/2019-07/products.json?limit=250&fields=title,vendor,handle"
Then in console paste this function (replace your-store).
(function getProducts(url = 'https://your-store.myshopify.com/admin/api/2019-07/products.json?limit=250&fields=title,vendor,handl...') {
      fetch(url).then(res => {
              const headerLink = res.headers.get('link');
              const match = headerLink?.match(/<[^;]+\/(\w+\.json[^;]+)>;\srel="next"/);
              const url = match ? match[1] : false;
              if(url){
                    res.json().then((data) => {
                    data.products.map((el) => {
                           console.log(JSON.stringify(el))
                    })
              })
              getProducts(url)
              } else {
                  res.json().then((data) => {
                  data.products.map((el) => {
                           console.log(JSON.stringify(el))
                 })
              })
      }
   })
})()

Gonkas
Tourist
11 0 1

And how do you call that Ufi?

 

Trying to call it like this:
ProductsGetListAsync(myShopifyUrl, shopAccessToken);

 

But gives me an error that it does not exist in the current context (CS0103).

 

Cheers,

Goncalo

Ufi
Shopify Partner
10 0 0

You need to DI the interface or create a class and the call the method

Gonkas
Tourist
11 0 1

Thanks Ufi, do you have any full example in C#?

 

Cheers,

Gonkas

Ufi
Shopify Partner
10 0 0

Check this article: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0 

  1. create interface IShopifyService and define a method "ProductsGetListByGraphQL"
  2. create class ShopifyService than inherits the interface.
  3. in the class copy the code I provided for product search with GraphQL

Then just use your interface in the Controller you use to fetch products from Shop