How to list more than 100 customers using Chargebee Python SDK?

222 Views Asked by At

I want to list all customers in Chargebee, and currently I have 101 customers. I use chargebee.Customer.list({'limit': 100}) because in the documentation it says that "optional, integer, default=10, min=1, max=100".

Documentation link: https://apidocs.chargebee.com/docs/api/customers?prod_cat_ver=2#list_customers

I have more than 100 customers but I still do not have "next_offset" returned.

May I ask how I can get all customers? Or 100 customers at first with an offset?

Thanks.

1

There are 1 best solutions below

0
pscheid On

May I ask how I can get all customers?

According to the Chargebee documentation for list operations (here), one has to repeatedly call the API until the next_offset field is missing in the response object. The offset parameter allows us to start where we left off.

One can use the limit parameter to ask Chargebee for more customers in one call, reducing the number of total calls we have to make.

I included a Python snippet as an example of how to implement pagination.

import chargebee


def get_all_customers(params, limit=100, offset=None):
    customers = list()

    params['limit'] = limit
    params['offset'] = offset

    while True:
        entries = chargebee.Customer.list(params)
        customers.extend(entries.response)

        if entries.next_offset:
            params['offset'] = entries.next_offset
        else:
            break

    return customers


def main():
    chargebee.configure('<api_token>', '<site>')
    customers = get_all_customers({"first_name[is]": "John"})
    for customer in customers:
        print(customer)


if __name__ == '__main__':
    main()


I have more than 100 customers, but I still do not have "next_offset" returned.

Unfortunately, we cannot provide help with that, as we cannot access your Chargebee environment and debug.