Shopify switched their API to "cursor-based pagination" in July of 2019. This eliminated the page
parameter that made it simple and obvious how to get multiple pages of results.
The Python wrapper for the Shopify API actually makes it quite difficult to page through results now. The API responds with next
and previous
URLs via the response headers that can be used to retrieve different pages of results, but the python wrapper actually drops these headers. There is a pull request waiting to be merged that will give access to convenient next
and previous
methods. In the meantime, maybe this will be useful to someone else...
- Shopify private app with API key and password
import shopify
SHOP_HANDLE = '<INSERT SHOP HANDLE>'
API_KEY = '<INSERT API KEY>'
PASSWORD = '<INSERT APP PASSWORD>'
API_VERSION = '2019-07'
def get_all_products(limit=100):
get_next_page = True
since_id = 0
while get_next_page:
products = shopify.Product.find(since_id=since_id, limit=limit)
for product in products:
yield product
since_id = product.id
if len(products) < limit:
get_next_page = False
def main():
shop_url = "https://{}.myshopify.com/admin/api/{}".format(SHOP_HANDLE, API_VERSION)
shopify.ShopifyResource.set_site(shop_url)
shopify.ShopifyResource.set_user(API_KEY)
shopify.ShopifyResource.set_password(PASSWORD)
for product in get_all_products():
# Do something with product
print(product.title)
if __name__ == "__main__":
main()