Access a community of over 900,000 Shopify Merchants and Partners and engage in meaningful conversations with your peers.
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?
Solved! Go to the solution
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.
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
Do you know if the GraphQL API allows for a single query to return ALL the products? or is it still throttled by pagination?
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
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.
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?
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;
}
}
}
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;
}
I try to do the same but it return 404 because the ? in url is converted to %3F
how to stop it from converting
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;
}
Hi @TxBob77 ,
some additional hints in respect to the REST API:
{'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
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))
})
})
}
})
})()
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
You need to DI the interface or create a class and the call the method
Thanks Ufi, do you have any full example in C#?
Cheers,
Gonkas
Check this article: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-6.0
Then just use your interface in the Controller you use to fetch products from Shop
User | RANK |
---|---|
5 | |
4 | |
4 | |
3 | |
3 |