How to get all product id from Shopify Python API
Update as of 2019-07: This will no longer work as it has been deprecated and subsequently removed from the Shopify API.
The replacement is detailed in this answer.
Original answer below
Shopify returns paginated responses for lists of resources. The default number of resources per page is 50
, and the default page is 1
. Your request is thus equivalent to the following:
shopify.Product.find(limit=50, page=1)
Shopify allows you to increase the limit per page to 250. Here's a helper function I use to get all of a given resource:
def get_all_resources(resource, **kwargs):
resource_count = resource.count(**kwargs)
resources = []
if resource_count > 0:
for page in range(1, ((resource_count-1) // 250) + 2):
kwargs.update({"limit" : 250, "page" : page})
resources.extend(resource.find(**kwargs))
return resources
You use it like this:
products = get_all_resources(shopify.Product)
You can even pass in parameters. Your question asks specifically for the product ID- if you limit the query to only return the IDs, this will be much, much faster (as it doesn't have to pull in any of the product variants):
product_ids = get_all_resources(shopify.Product, fields="id")
Note that if you have 2.4k products, this may take some time!
Documentation: https://help.shopify.com/api/reference/product
An extension of Lennart Rolland response.
As he stated the preferred answer is no longer applicable in api version 2019-07.
I was unable to get his code sample to work, because of an error "list does not have the function has_next_page()".
So I have written an example using the 'Link' header rel='next' pagination.
def get_all_resources(resource):
page_info = str()
resources = list()
while True:
resources.extend(resource.find(limit=250, page_info=page_info))
cursor = shopify.ShopifyResource.connection.response.headers.get('Link')
if 'next' in cursor:
page_info = cursor.split(';')[-2].strip('<>').split('page_info=')[1]
else:
break
return resources
Pagination interface has changed in Shopify API, and the old "limit + page" way of doing pagination was removed in api version 2019-07.
In other words: the accepted answer by @Julien will NOT work in this api version and later.
I have re-created the function of the accepted answer using the new way of doing relative cursor based pagination here:
def get_all_resources(resource_type, **kwargs):
resource_count = resource_type.count(**kwargs)
resources = []
if resource_count > 0:
page=resource_type.find(**kwargs)
resources.extend(page)
while page.has_next_page():
page = page.next_page()
resources.extend(page)
return resources