# Not Getting All Contacts

**URL:** <https://community.hubspot.com/t/not-getting-all-contacts/25282>\
**Category:** APIs & Integrations\
**Tags:** developer-tools, contacts\
**Created:** [September 10, 2020, 3:35am UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282 "2020-09-10T03:35:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [September 10, 2020, 3:35am UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/1 "2020-09-10T03:35:38Z")

</div>

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.

```lia-code-sample
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.

---

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [September 10, 2020, 5:57pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/2 "2020-09-10T17:57:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [September 10, 2020, 6:17pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/3 "2020-09-10T18:17:42Z")

</div>

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](https://legacydocs.hubspot.com/docs/methods/contacts/get_contacts) 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

---

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [September 10, 2020, 9:51pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/4 "2020-09-10T21:51:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [September 11, 2020, 4:15am UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/5 "2020-09-11T04:15:58Z")

</div>

I was able to get it to work.

Dennis, thanks again for you help!

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [September 11, 2020, 5:35pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/6 "2020-09-11T17:35:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [September 14, 2020, 7:32pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/7 "2020-09-14T19:32:10Z")

</div>

@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).

```lia-code-sample
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...')

```

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [September 14, 2020, 9:03pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/8 "2020-09-14T21:03:22Z")

</div>

@taran42 you are a champ!

Thanks for posting

---

<div class="post-metadata">

**Author:** ![EJoyce2](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ejoyce2/32/41549_2.png) [@EJoyce2](https://community.hubspot.com/u/EJoyce2)\
**Post date:** [August 25, 2021, 4:58pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/9 "2021-08-25T16:58:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [August 25, 2021, 5:57pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/10 "2021-08-25T17:57:51Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![EJoyce2](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ejoyce2/32/41549_2.png) [@EJoyce2](https://community.hubspot.com/u/EJoyce2)\
**Post date:** [August 25, 2021, 7:13pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/11 "2021-08-25T19:13:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![taran42](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/taran42/32/105524_2.png) [@taran42](https://community.hubspot.com/u/taran42)\
**Post date:** [August 25, 2021, 7:21pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/12 "2021-08-25T19:21:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [August 26, 2021, 1:29pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/13 "2021-08-26T13:29:40Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![EJoyce2](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ejoyce2/32/41549_2.png) [@EJoyce2](https://community.hubspot.com/u/EJoyce2)\
**Post date:** [August 26, 2021, 7:38pm UTC](https://community.hubspot.com/t/not-getting-all-contacts/25282/14 "2021-08-26T19:38:43Z")

</div>

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
