How to implement pagination with 'after' parameter in HubSpot API using Node.js

I’m working on integrating with HubSpot’s API and I’m struggling with implementing proper pagination for retrieving contact records. I’m using the official HubSpot Node.js client library.

hubspotAPI.crm.contacts.basicApi
  .getPage(pageLimit, afterToken, fields, fieldsWithHistory, relationships, includeDeleted)
  .then((response) => {
    console.log(response)
  })
  .catch((error) => {
    console.error(error)
  })

I understand that the afterToken parameter accepts a contact ID and returns records starting from that specific ID onwards. However, I’m not sure how to properly implement pagination logic using this approach. Should I be storing the last contact ID from each response and using it as the afterToken for the next request? Is there a better way to handle pagination with HubSpot’s API, or am I on the right track with this method?

Yeah, that’s exactly how HubSpot’s pagination works. I’ve used their API for two years and storing the last contact ID is definitely the right approach. Just a heads up - their rate limits are pretty strict, so add delays between requests. I’d also wrap your pagination in try-catch blocks because the API can be flaky with large datasets. Use the paging.next.after token from the response for your next call. Keep going until that token’s empty and you’re done.

yea, u got it! just store that last contact id. fetch the paging.next.after from the response and use it for ur next request. loop till that value is undefined or null - that’s when ur done.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.