When calling Stripe::Customer.all(:limit => 100) there is a limit of 100 per call. We have a lot more customers than that, and I would like to get them all at once. Am I missing something, or is this only possible by writing a naive loop that checks on the has_more attribute and then makes a new call until has_more = false?

up vote 9 down vote accepted

You are right, you must write a naive loop with a cursor per the stripe docs:

starting_after

optional

A cursor for use in pagination. starting_after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include starting_after=obj_foo in order to fetch the next page of the list.

Here's one in case anyone needs a quick copy-paste.

  def self.all_stripe_customers
    starting_after = nil
    customers = []
    loop
      results = Stripe::Customer.all(limit: 100, starting_after: starting_after)
      break if results.data.length == 0
      customers = customers + results.data
      starting_after = results.data.last.id  
    end
    return customers
  end
  • 1
    Thanks for posting, was helpful! I did make one important change though, this code (almost) always makes one more query than it has to. Instead it should check results.has_more in the while condition. – user12341234 May 25 '15 at 16:27
  • one addition - you probably want to rescue from Stripe::RateLimitError and retry after waiting an appropriate amount of time – Phil Aug 16 '16 at 20:06
  • should be loop do – YWCA Hello Aug 15 at 21:59

Probably a little late to the game here, but here's a yielding version of Doug's answer which loops through every customer, yielding them to a block:

def each_stripe_customer
  starting_after = nil
  loop
    customers = Stripe::Customer.all(limit: 100, starting_after: starting_after)
    break if customers.data.length == 0
    customers.each do |customer|
      yield customer
    end
    starting_after = customers.data.last.id  
  end
end

You can use this as follows:

each_stripe_customer do |customer|
  puts customer.id
end

This abstracts away the fetching of customers from however you actually want to use them.

I would suggest to keep a local copy of customer data. so you get the data from Stripe API only once & use that local copy instead of asking that same data again-again.

For syncing changes, There are two main approaches you might use to make sure your database has the latest state:

The primary benefit to storing this data locally apart from getting around API limitations, is speed.

You will want to make sure you're storing the customer ID in your database so that you can match up the Stripe customer with your local user.

Hope it helps

  • 2
    This may be good advice, but it doesn't answer the question - and you have no reason to think that the OP isn't already doing what you advise. Even if you have a local copy of the data, it's quite possible that you'll need to pull everything directly out of Stripe from time to time either to fix up bugs or gaps in recording your local copy, or for auditing purposes. Plus anyone who hasn't yet taken this advice needs an answer to the question that was asked in order to set up their local copy to start working from. This answer can't possibly help anybody on its own. – Mark Amery Sep 15 '15 at 16:24

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.