0

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.

Peijia Yu
  • 1
  • 1
  • Have you checked the bug tracker? Also, can you link the documentation? As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Jan 03 '23 at 13:28
  • The documentation for chargebee python sdk has already been linked – Peijia Yu Jan 12 '23 at 08:40
  • I think if you want more than 100, you should not add 'limit': 100 in your request. By adding it, you say to the API server that you have reached your max. If nothing is added, API will know that your have more than 100 then send the first 100 hundreds with a next_token. – behanzin777 Jan 12 '23 at 08:59

1 Answers1

0

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.

pscheid
  • 450
  • 4
  • 10