List all customers via stripe API

Question:

I’m trying to get a list of all the customers in my stripe account but am limited by pagination, wanted to know what the most pythonic way to do this.

customers = []
results = stripe.Customer.list(limit=100)
print len(results.data)
for c in results:
    customers.append(c)
results = stripe.Customer.list(limit=100, starting_after=results.data[-1].id)
for c in results:
    customers.append(c)

This lists the first 200, but then how do I do this if I have say 300, 500, etc customers?

Asked By: nadermx

||

Answers:

Stripe’s Python library has an "auto-pagination" feature:

customers = stripe.Customer.list(limit=100)
for customer in customers.auto_paging_iter():
    # Do something with customer

The auto_paging_iter method will iterate over every customer, firing new requests as needed in the background until every customer has been retrieved.

The auto-pagination feature is documented here.

Answered By: Ywain
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.