Below code to implement pagination is not working while fetching new orders from Shopify

Topic summary

A developer is experiencing issues implementing pagination when fetching orders from Shopify’s API. Their code successfully retrieves the first 250 orders but fails to fetch subsequent pages.

Current Setup:

  • Ruby 2.6.10
  • Rails > 6.1.5
  • shopify_api gem > 0.5.21
  • Using ShopifyAPI::Order.all() with page_info and next_page_info for pagination

Problem:
The ShopifyAPI::Order.next_page? method returns false even though thousands of orders exist in the store beyond the initial 250. No errors are thrown, but the loop terminates prematurely without fetching additional pages.

Attempted Solution:
Another user (SBD) suggested alternative code using a loop structure that checks ShopifyAPI::Order.next_page? and reassigns orders using next_page_info. The original poster confirmed the store has multiple pages of orders but the pagination mechanism isn’t working as expected despite following the official Shopify API Ruby documentation.

Summarized with AI on November 14. AI used: claude-sonnet-4-5-20250929.
LIMIT=250

def fetch_shopify_orders(status="paid") 
ShopifyAPI::Order.all(session: @session, financial_status: status, updated_at_min: Time.current.to_date - 2.days, limit: LIMIT)
end

def fetch_next_shopify_orders 
ShopifyAPI::Order.all(session: @session, page_info: ShopifyAPI::Order.next_page_info, limit: LIMIT)
end

orders = fetch_shopify_orders
while ShopifyAPI::Order.next_page?
orders = fetch_next_shopify_orders
end

I have taken reference from here https://shopify.github.io/shopify-api-ruby/usage/rest.html.

Using configuration mentioned below.
ruby ‘2.6.10’
gem ‘rails’, ‘> 5.1.6’
gem ‘shopify_api’, ‘> 12.5.0’

Please help me to resolve this issue, any help would be highly appreciated.

Hey @rakesh11

Do you get a specific error? What’s the output? Have you confirmed the store has multiple pages of orders?

Hi

Not getting any error, the above query returns top 250 orders and after that it does not return the next 250 orders.

Yes there are thousands of orders in store. I also tried to check this variable “ShopifyAPI::Order.next_page?” and it returns false but there are many orders below top 250 orders.

The script which is mentioned above should return all orders in one execution, but unfortunately it is not happening for me.

Thanks

Hey @rakesh11

Can you give this code a shot?

orders = ShopifyAPI::Order.all(session: session, limit: 250)

loop do
  puts orders
  break unless ShopifyAPI::Order.next_page?
  orders = ShopifyAPI::Order.all(session: session, limit: 250, page_info: ShopifyAPI::Order.next_page_info)
end