Python - Getting Custom Contacts Information

Hi All,

I am just starting out with HubSpot and am after some help with gathering data using Python.

I am currently using the below code to gather custom objects which I will eventually expand.

The below code works for gathering information when there only enough contacts for 1 page, though wont gather information if the number of contacts exceeds this?

Spoiler

import pandas as pd

from hubspot import HubSpot

from hubspot.crm.contacts.exceptions import ApiException

from pandasgui import show

def extract_all_contacts():

api_client = HubSpot(access_token=‘insert-token-here’)

custom_property_names = [‘firstname’, ‘lastname’]

df = pd.DataFrame()

params = {“limit”: 100, “archived”: False}

all_contacts = []

while True:

try:

contacts_page = api_client.crm.contacts.get_all(**params)

all_contacts.extend(contacts_page)

except ApiException as e:

print(“Exception when requesting Contacts by id: %s\n” % e)

break

if not contacts_page:

break

for contact in contacts_page:

contact_properties = {

name: contact.properties.get(name, None) for name in custom_property_names

}

df_properties = pd.DataFrame([contact_properties])

df = pd.concat([df, df_properties], ignore_index=True)

if len(contacts_page) < params[“limit”]:

break

params[“after”] = contacts_page[-1].id

show(df)

extract_all_contacts()

so with the above code I am getting the following error:
TypeError: hubspot.crm.contacts.api.basic_api.BasicApi.get_page() got multiple values for keyword argument ‘after’
anyone any ideas?

Hi @Common

Taking a peak at the Python library here: hubspot-api-python/README.md at master · HubSpot/hubspot-api-python · GitHub

It does mention that

api_client.crm.contacts.get_all

already includes pagination. It states: “Please note that pagination is used under the hood to get all results.” So this may be why you’re receiving just the first page?

I would recommend trying this instead:

client.crm.contacts.default_api.get_page(limit=100, archived=False)

Then you can read through each page.

I’ve noticed sometimes the client doesn’t work with every single request, so sometimes just referring to the API docs and using the direct endpoint is better.

Hope this helps!

Thank You!