How to retrieve complete contact list from HubSpot API without 250 record limit

I’m trying to pull all contacts from a specific HubSpot list through their API, but I keep hitting a wall. The API only gives me 250 contacts at most, even though my list has way more entries.

I tried using the ‘vidOffset’ parameter to get the next batch of results, but it’s not working as expected. My goal is to sync all contacts from HubSpot into my local database.

Here’s the PHP code I’m working with:

function fetchData($requestType, $endpoint, $params){
   $ch = curl_init();
   $endpoint = $endpoint+'&property=lastname&property=phone&limit=10&offset=5';
    switch ($requestType){
      case "POST":
         curl_setopt($ch, CURLOPT_POST, 1);
         if ($params)
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
         break;
      case "PUT":
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
         if ($params)
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);                              
         break;
      default:
         if ($params)
            $endpoint = sprintf("%s?%s", $endpoint, http_build_query($params));
   }
   curl_setopt($ch, CURLOPT_URL, $endpoint);
   curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
   ));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

   $response = curl_exec($ch);
   if(!$response){die("Request Failed");}
   curl_close($ch);
   return $response;
}

// execute the request
fetchData('GET', 'https://api.hubapi.com/contacts/v1/lists/25/contacts/all?hapikey=[YOUR_KEY]', false);

Am I missing something in my approach? Has anyone found a reliable method to extract all contacts using PHP or WordPress integration?

You’re mixing parameters in your endpoint URL. Avoid hardcoding offset=5 and limit=10 directly in the URL string, as it disrupts pagination logic. Additionally, use ‘.’ for string concatenation in PHP instead of ‘+’, which can lead to errors. To effectively manage pagination with the contacts API, establish a loop that maintains your offset value, incrementing it by your limit each time. Continue querying the API until you receive an empty array or fewer records than your limit. Incorporate error handling and rate limiting to prevent exceeding your API quotas. The key is to consistently track the offset in your loop rather than relying on fixed values.

You’re experiencing issues because the old v1 contacts API is being used. The vidOffset parameter was removed in v3, which uses pagination tokens instead. You should switch to the v3 endpoint: https://api.hubapi.com/crm/v3/objects/contacts. Instead of using vidOffset, employ the after parameter; your first request will return a paging object with a next token, which you need for subsequent requests. Additionally, there’s a syntax error in your code where you are using + for string concatenation instead of . in PHP. Finally, note that v3 allows you to pull up to 100 records per request, reducing the number of API calls needed.

Been there! Your main problem is the hardcoded limit=10 in your endpoint URL - it’s overriding whatever limit you’re passing as a parameter. Plus that v1 API is deprecated, so you’ll hit weird bugs. Remove the limit from your URL string and set up proper pagination with a while loop that keeps going until there’s no more data.