I need help fetching all contacts from my HubSpot account using their API. The problem is that each API request only returns a maximum of 100 contacts per call.
I want to create a loop that will continue making API calls until I’ve retrieved every contact from my account. How can I implement pagination to keep fetching the next batch of contacts until there are no more results? I need to know when to stop the loop once all contacts have been collected.
I’ve been using HubSpot’s API for two years and pagination was a pain at first. Your code looks good, but you need to fix how it handles continuation tokens. HubSpot sends back an after parameter when there’s more data. I use a while loop that keeps going as long as there’s an after value in the paging object. Start with after=None on your first call, grab the new after from each response, then pass it to the next request. Heads up - the API sometimes returns empty pages at the end of big datasets, so handle those in your loop. Also, if your team’s updating contacts while you’re pulling data, those properties might change between calls. Keep that in mind for your processing logic.
Here’s how to get all HubSpot contacts with pagination: Start with your first request and look for the paging property in the response. If there’s a next object with an after token, use that token in your next API call. Keep doing this - grab the new after value from each response and use it for the next request. You’re done when there’s no paging.next object in the response. I’d recommend collecting all contacts in a list as you go, adding some error handling for network issues, and throwing in small delays between requests so you don’t hit rate limits (especially with large contact lists).
It’s pretty straightforward once you get the response structure. Your first request returns an object with contacts data and pagination info. Just grab the after cursor from response.paging.next.after when it’s there. I set up a contacts list outside the loop, then use a simple while True that breaks when there’s no next page. Each loop adds the current batch to your main list and updates the after parameter for the next call. Heads up - HubSpot sometimes sends duplicate contacts across pages if they’re getting modified while you’re pulling data. If you need clean data, dedupe by contact ID.
just keep checking for that next_after token in your loop. grab response.paging.next.after and use it in your next call. when there’s no more, that’s when you stop. it’s not too complicated once you get the hang of it!
just check the has_more in the response, and keep using the after parameter. loop while has_more is true and take the after value from paging, then pass it to your next call. also, be careful with rate limits – hubspot can be picky!