APIs & Integrations

SCohen3
Member

Get a big list of Companies

Hello, I am new to Hubspot please help me with this issue.

I am trying to get all my 4,809 companies through the following URI:

 

 

CreateCompURI = "https://api.hubapi.com/companies/v2/companies/paged?hapikey=" + DestinationAPIKey + "&limit=100&properties=name&properties=account_number";

 

 

Once the first call is made I get a hasmore bool and an offset value:

 

 

"has-more": true,
    "offset": 8018991168

 

 

Which I then use in my next URI:

 

 

CurrentCompanyURI = "https://api.hubapi.com/companies/v2/companies/paged?hapikey=" + DestinationAPIKey + "&offset=" + offset + "&limit100&properties=name&properties=account_number";

 

 

However, after multiple iterations on a while loop using the hasmore bool and the offset, the loop is infinite and the list keeps growing.

 

The bool should return false when there are no more companies to GET. However, the bool keeps as true.

 

This is the method that I am using on C#:

 

 

public List<HubSpotCurrentComnpany> GetCurrentCompanies()
{
    List<HubSpotCurrentComnpany> companies = new List<HubSpotCurrentComnpany>();
    bool hasmore = false;

    try
    {
        do
        {
            using (var client = new HttpClient())
            {
                if (offset != 0)
                {
                    CurrentCompanyURI = CurrentCompanyURI.Replace("offset=0", "offset=" + offset);
                    client.BaseAddress = new Uri(CurrentCompanyURI);
                }
                else
                {
                    client.BaseAddress = new Uri(CreateCompURI);

                }

                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AccessToken);

                var response = client.GetAsync("").Result;
                var readTask = response.Content.ReadAsStringAsync().Result;
                Company_ResponseModel result =
                    JsonConvert.DeserializeObject<Company_ResponseModel>(readTask,
                    new JsonSerializerSettings()
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });

                foreach (var company in result.companies)
                {
                    companies.Add(new HubSpotCurrentComnpany()
                    {
                        companyids = Convert.ToString(company.companyId),
                        name = company.properties?.name?.value,
                        account_number = company.properties?.account_number?.value
                    });

                }
                if (result.HasMore)
                {
                    hasmore = true;
                    offset = result.offset;
                }
                else
                {
                    hasmore = false;
                    offset = 0;
                }
            }
        } while (hasmore);
        return companies;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

 

 

 

How can I get all my 4,809 companies? What am I doing wrong?

 

Thank you.

0 Upvotes
1 Reply 1
dennisedson
HubSpot Product Team
HubSpot Product Team

Get a big list of Companies

@SCohen3 

Have you attempted to use the newer v3 endpoint?

You can find the docs here

0 Upvotes