Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all customers via stripe API

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?

like image 445
nadermx Avatar asked Jan 05 '23 08:01

nadermx


1 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 (you have to scroll down a bit).

like image 97
Ywain Avatar answered Jan 08 '23 07:01

Ywain