You can't get more than 250 products with a single request from Shopify.
Refer to the docs here: https://shopify.dev/docs/admin-api/rest/reference/products/product?api[version]=2020-04 ( where the limit max value can be 250 )
In order to get more than 250 products you need to make a recursive function and use the page_info argument to make paginated requests. More on the matter can be seen here: https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api
When you make a request and there is a pagination shopify returns a header similiar to this one:
Link: "<https://{shop}.myshopify.com/admin/api/2019-07/products.json?page_info=vwxyzab&limit=6>; rel=next"
In order to make a request to the second page you need to grab the link and make a request to it, the same applies when you make that request, there will be the same header if there are more pages and so on.
So you need to get your response header and the link from it and make it recursive:
function makeRequest(nextLink = '{STORE URL}/products.json?limit=250'){
return new Promise((resolve, reject) => {
fetch(nextLink).then(r => {
const headerLink = r.headers.get('link');
const match = headerLink.match(/<[^;]+\/(\w+\.json[^;]+)>;\srel="next"/);
const nextLink = match ? match[1] : false;
if(nextLink){
makeRequest(nextLink)
} else {
resolve();
}
})
})
}
Answer from drip on Stack OverflowYou can't get more than 250 products with a single request from Shopify.
Refer to the docs here: https://shopify.dev/docs/admin-api/rest/reference/products/product?api[version]=2020-04 ( where the limit max value can be 250 )
In order to get more than 250 products you need to make a recursive function and use the page_info argument to make paginated requests. More on the matter can be seen here: https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api
When you make a request and there is a pagination shopify returns a header similiar to this one:
Link: "<https://{shop}.myshopify.com/admin/api/2019-07/products.json?page_info=vwxyzab&limit=6>; rel=next"
In order to make a request to the second page you need to grab the link and make a request to it, the same applies when you make that request, there will be the same header if there are more pages and so on.
So you need to get your response header and the link from it and make it recursive:
function makeRequest(nextLink = '{STORE URL}/products.json?limit=250'){
return new Promise((resolve, reject) => {
fetch(nextLink).then(r => {
const headerLink = r.headers.get('link');
const match = headerLink.match(/<[^;]+\/(\w+\.json[^;]+)>;\srel="next"/);
const nextLink = match ? match[1] : false;
if(nextLink){
makeRequest(nextLink)
} else {
resolve();
}
})
})
}
Two cases are here:
Case 1: You have the admin access. You can simply paste the below code snippet into your theme.liquid file and get all product data in browser console.
<script>
const total_products = {{- shop.products_count | default : false -}};
if(total_products){
let storeProducts = [];
let fetchCalls = [];
let ceailedCount = Math.ceil(total_products/250);
for (let i =1 ; i <= ceailedCount; i++ ){
fetchCalls.push(fetch('/products.json/?limit=250&page='+i))
}
Promise.all(fetchCalls)
.then(m => m.forEach(s => {
let k = s.json();
k.then( s => { storeProducts.push(s.products) })
}
)).then( j => console.log('process Completed',storeProducts ));
}
</script>
Case 2: You are viewing the website as a customer. In this case, you must first know how many total products are in the store, which you can get by making the below Ajax calls within the browser console. All you have to do is run the below fetch calls one by one in the browser console until less than 250 products or no product array is returned.
fetch('/products.json?limit=250&page=1').then(r => r.json()).then( p => console.log(p));
fetch('/products.json?limit=250&page=2').then(r => r.json()).then( p => console.log(p))
fetch('/products.json?limit=250&page=3').then(r => r.json()).then( p => console.log(p))
fetch('/products.json?limit=250&page=4').then(r => r.json()).then( p => console.log(p))
fetch('/products.json?limit=250&page=5').then(r => r.json()).then( p => console.log(p))
//and so on... until you are getting product results.
Note that; all the fetch calls are the same, just the page number is different. Each Fetch call returns us up to 250 products. When you keep making fetch calls in the same order, and the last fetch call will return either no product or less than 250 products. Which is the last fetch call you have to make. Consequently, you know the total number of products, ( 250 times page number ). Note the page number of that last call and then run the below code ( REPLACE THE PAGE_NUMBER WITH YOUR NUMBER )
let storeProducts = [];
let fetchCalls = [];
let ceailedCount = PAGE_NUMBER;
for (let i =1 ; i <= ceailedCount; i++ ){
fetchCalls.push(fetch('/products.json/?limit=250&page='+i))
}
Promise.all(fetchCalls)
.then(m => m.forEach(s => {
let k = s.json();
k.then( s => { storeProducts.push(s.products) })
}
)).then( j => console.log('process Completed',storeProducts ));
Finally, you will get the all products in an array storeProducts
To get the Shopify Product Id and Product Variant ID, you can use Shopify REST API. Since, you already have the NodeJS application you can use the Shopify API Node.js Module. Just fetch all products, pass fetched data to frontend and then use the scripts mentioned in your question to render Shopify Buy button.
Sample code to get all products
const Shopify = require('shopify-api-node');
const shopify = new Shopify({
shopName: 'store-url.myshopify.com',
apiKey: 'xxxxxxxxxxxxxxxx',
password: 'xxxxxxxxxxxxxx',
autoLimit: true
});
shopify.product.count()
.then(async (count) => {
if (count > 0) {
const pages = Math.ceil(count / 250);
let products = [];
for (i = 0; i < pages; i++) {
// use Promise.all instead of waiting for each response
const result = await shopify.product.list({
limit: 250,
page: i + 1,
fields: 'id, variants'
});
products = products.concat(result);
}
// products array should have all the products. Includes id and variants
console.log(products);
}
})
.catch(err => {
console.log(err);
});
For better performance, consider saving products in database and update information periodically.
Shopify REST API
This solution works with shopify-api-node in 2023:
let products: IPaginatedResult<IProduct> = []
let page_info: string | undefined = undefined
// Get all products
// page_info is the next page ID to fetch
// https://shopify.dev/docs/api/usage/pagination-rest
do {
const newProducts = await shopify.product.list({
limit: 250, // max is 250
page_info
})
products = [...products, ...newProducts]
page_info = newProducts.nextPageParameters?.page_info
} while (page_info)
return products