APIs & Integrations

taran42
Contributor

Not Getting All Contacts

SOLVE

I am trying to get a list of my contacts from the Hubspot API via Python. When I run the script, it only returns 19 (out of a couple of thousand) contacts. Below is my code.

 

import requests

url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=myapikey&property=email"

response = requests.request("GET", url)

print(response.text)

 

I was wondering if it had to do with the number of properties I was requesting, so I pared it down to one, but I am still only getting 19 results, regardless of what I do.

0 Upvotes
1 Accepted solution
dennisedson
Solution
HubSpot Product Team
HubSpot Product Team

Not Getting All Contacts

SOLVE

Hello again @taran42 😀

We are fast becoming friends!

 

I am going out on a limb and saying you probably got 20 instead of 19.  20 is the default amount returned if you do not add the count query parameter

 

Please also note that you can only return a maximum of 100 per call. 

 

Also, from the docs:

Note: There are 2 fields here to pay close attention to: the "has-more" field that will let you know whether there are more contacts that you can pull from this account, and the "vid-offset" field which will let you know where you are in the list of contacts. You can then use the "vid-offset" field in the "vidOffset" parameter.

 

You have probably already looked the doc on this, but just in case, here is a link to the that endpoints documentation

 

This is an example of what your get request should look like

https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=XXXXX&count=100&vidOffset=0

 

Hope all is clicking for you! 

d

View solution in original post

0 Upvotes
13 Replies 13
EJoyce2
Participant

Not Getting All Contacts

SOLVE

Sweet thank you! Is there a way to include Associations in this get request? I am trying to get the Associated Company ID's with each contact

0 Upvotes
dennisedson
Solution
HubSpot Product Team
HubSpot Product Team

Not Getting All Contacts

SOLVE

Hello again @taran42 😀

We are fast becoming friends!

 

I am going out on a limb and saying you probably got 20 instead of 19.  20 is the default amount returned if you do not add the count query parameter

 

Please also note that you can only return a maximum of 100 per call. 

 

Also, from the docs:

Note: There are 2 fields here to pay close attention to: the "has-more" field that will let you know whether there are more contacts that you can pull from this account, and the "vid-offset" field which will let you know where you are in the list of contacts. You can then use the "vid-offset" field in the "vidOffset" parameter.

 

You have probably already looked the doc on this, but just in case, here is a link to the that endpoints documentation

 

This is an example of what your get request should look like

https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=XXXXX&count=100&vidOffset=0

 

Hope all is clicking for you! 

d

0 Upvotes
taran42
Contributor

Not Getting All Contacts

SOLVE

Hello @dennisedson ! I welcome more friends who are knowledgeable at this.

 

You are correct; I am getting 20, not 19 (I wasn't counting 0). I did find out, after making the original post, that I could only get a maximum of 100 contacts per request.

 

Using vidOffset, I was able to return more contacts. Thank you for that. However, now I can't figure out how to use has-more to get all of my contacts. I know it needs to go in some kind of While function and loop, but I can't seem to get it to work. Any pointers there?

taran42
Contributor

Not Getting All Contacts

SOLVE

I was able to get it to work.

 

Dennis, thanks again for you help!

EJoyce2
Participant

Not Getting All Contacts

SOLVE

Any way you can let me know how you got this to work? I am trying to loop through deals, contacts, and companies but unsure how to set up a while function / loop. Let me know thanks

0 Upvotes
taran42
Contributor

Not Getting All Contacts

SOLVE

@EJoyce2 Hubspot calls only return a particular number of values. You need to use vidOffset to get the others, once the maximum number is reached. Refer to my code post above for how I specifically acheived this. Note the max_results and count and the important part being has_more under pagination.

EJoyce2
Participant

Not Getting All Contacts

SOLVE

do you know how this could work for a custom object? 

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Not Getting All Contacts

SOLVE

@EJoyce2 ,

Once you create a custom object, the same rules apply as other objects.  So if you have more than you can get in one call, you should see an vid offset returned.

I would suggest testing in postman or equivalent so you can see.

0 Upvotes
taran42
Contributor

Not Getting All Contacts

SOLVE

What do you mean by custom object? The process I created will only work for contacts, companies and deals. Is it a custom property of one of the aforementioned categories?

dennisedson
HubSpot Product Team
HubSpot Product Team

Not Getting All Contacts

SOLVE

hey @taran42 !

Glad it is working out.  Would be really cool if you could post your solution here for others to see.  I feel like some more python specific code would be of benefit to all!

 

Thanks again!

d

taran42
Contributor

Not Getting All Contacts

SOLVE

@dennisedson Sure thing! Below is the code I used to get all of my contacts in Hubspot. It's mostly what is provided in the documentation you linked to, but with a few changes. Most notably the change in the pull from urllib, using urllib.parse.urlencode in the while loop (the documentation uses urllib v1 and currently it's urllib v3) and json.dumps (Python pushes out malformed JSON code, using single quotes instead of double, and this puts doubles in, which JSON requires).

 

import requests
import json
import urllib

# Maximum number of results to cycle through
max_results = 1000
hapikey = 'myAPIkey'
# Specify the number of contacts to return in the API call (default is 20, max is 100)
count = 10
contact_list = []
property_list = []
# Set the properties to retrieve in the URL (&property=prop_name)
get_all_contacts_url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=myAPIkey&property=list_of_properties&"
parameter_dict = {'hapikey': hapikey, 'count': count}
headers = {}

# Paginate your request using offset
has_more = True
# has_more lets the program know if there are more contacts to cycle through
while has_more:
	parameters = urllib.parse.urlencode(parameter_dict)
	get_url = get_all_contacts_url + parameters
	r = requests.get(url= get_url, headers = headers)
	response_dict = json.loads(r.text)
	has_more = response_dict['has-more']
	contact_list.extend(response_dict['contacts'])
	parameter_dict['vidOffset']= response_dict['vid-offset']
    # Exit pagination, based on what ever value you've set your max results variable to
	if len(contact_list) >= max_results:
		print('maximum number of results exceeded')
		break
print('loop finished')

list_length = len(contact_list)

print("You've succesfully parsed through {} contact records and added them to a list".format(list_length))

# Adds double quotes " to JSON output that Python strips and uses single quotes isntead '
print(json.dumps(contact_list))

input('Press Enter key to exit...')

 

dennisedson
HubSpot Product Team
HubSpot Product Team

Not Getting All Contacts

SOLVE

@taran42 you are a champ! 

Thanks for posting

0 Upvotes
taran42
Contributor

Not Getting All Contacts

SOLVE

In trying to find a solution to my problem, I performed a count of my contacts.

 

url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=myapikey&count=1000"

 

This only returned 249 contacts. I expected it to give me 1000 contacts (I have over 2000). Is there some criteria I am missing in my URL string?

0 Upvotes