# Python KeyError When Trying to Get All Companies

**URL:** <https://community.hubspot.com/t/python-keyerror-when-trying-to-get-all-companies/26452>\
**Category:** APIs & Integrations\
**Tags:** companies, developer-tools\
**Created:** [October 6, 2020, 5:00pm UTC](https://community.hubspot.com/t/python-keyerror-when-trying-to-get-all-companies/26452 "2020-10-06T17:00:03Z")\
**Posts on this page:** 4\
**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:** [October 6, 2020, 5:00pm UTC](https://community.hubspot.com/t/python-keyerror-when-trying-to-get-all-companies/26452/1 "2020-10-06T17:00:03Z")

</div>

I am trying to use Python to get all of my Company contacts. However I keep getting a KeyError for the “has-more” function. From what I’ve read elsewhere, I think it actually has something to do with the “response\_dict” being blank, but I’m not sure how to fix it.

```lia-code-sample
import requests
import json
import urllib

max_results = 10 
hapikey = 'myAPIkey' 
limit = 10 
company_list = []
get_all_companies_url = "https://api.hubapi.com/companies/v2/companies/paged?hapikey=myAPIkey"
parameter_dict = {'hapikey': hapikey, 'limit': limit}
headers = {}

# Paginate your request using offset
has_more = True
while has_more:
	parameters = urllib.urlencode(parameter_dict)
	get_url = get_all_companies_url + parameters
	r = requests.get(url= get_url, headers = headers)
	response_dict = json.loads(r.text)
	has_more = response_dict['has-more']
	company_list.extend(response_dict['companies'])
	parameter_dict['offset']= response_dict['offset']
	if len(company_list) >= max_results: # Exit pagination, based on whatever value you've set your max results variable to.
		print('maximum number of results exceeded')
		break

print('loop finished')

list_length = len(company_list) 

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

```

The error I am getting is this:

```lia-code-sample
PS C:\Users\hubspot> & "C:/Program Files/Python38/python.exe" "c:/Users/Dropbox/Hubspot/companies.py" 
Traceback (most recent call last):
 File "c:/Users/Droppbox/Hubspot/companies.py", line 30, in <module>
KeyError: 'has-more'

```

---

<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:** [October 7, 2020, 7:52pm UTC](https://community.hubspot.com/t/python-keyerror-when-trying-to-get-all-companies/26452/2 "2020-10-07T19:52:21Z")

</div>

Hello @taran42 !

Long time no chat. As you know, I am not expert at Python, but I am wondering if you can add an if statement within that loop basically saying if has-more is null, then has-more = false…

@akaiser , @khookguy , @wfong ! Hello my Python friends! Do you guys have thoughts on this? I also don’t mind if you all look at my answer and start a private chat to laugh at me 😅 for suggesting something so outrageous!

Best!

---

<div class="post-metadata">

**Author:** ![wfong](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wfong/32/68653_2.png) [@wfong](https://community.hubspot.com/u/wfong)\
**Post date:** [October 7, 2020, 8:52pm UTC](https://community.hubspot.com/t/python-keyerror-when-trying-to-get-all-companies/26452/3 "2020-10-07T20:52:40Z")

</div>

I think it is failing on the dictionary look up since there is no has-more field being returned on an error response of the get request.

I would double check the request is returning data and not an failed response.

I see the line:

get\_all\_companies\_url = “[https://api.hubapi.com/companies/v2/companies/paged?hapikey=myAPIkey](https://api.hubapi.com/companies/v2/companies/paged?hapikey=myAPIkey)”

but shouldn’t it be only

get\_all\_companies\_url = “[https://api.hubapi.com/companies/v2/companies/paged?](https://api.hubapi.com/companies/v2/companies/paged?)”

You already have this part of the code which contains the hapikey.

parameter\_dict = {‘hapikey’: hapikey, ‘limit’: limit}

That’s my guess!

---

<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:** [October 13, 2020, 6:22pm UTC](https://community.hubspot.com/t/python-keyerror-when-trying-to-get-all-companies/26452/4 "2020-10-13T18:22:02Z")

</div>

@dennisedson That is kind of what I thought as well, and I tried that, but it didn’t work.

@wfong Yes, something was wrong with the dictionary. That was my thought too. Also, you are correct, I did not need my API key in the URL.

Eventually I was able to figure it out. There were a few things wrong with the while loop in regards to the response\_dict call. I needed _.get_ after the _response\_dict_ was called in three places (and a little bit of syntax fixing). See below.

```lia-code-sample
import requests
import json
import urllib

max_results = 10 
hapikey = 'myAPIkey' 
limit = 10 
company_list = []
get_all_companies_url = "https://api.hubapi.com/companies/v2/companies/paged?"
parameter_dict = {'hapikey': hapikey, 'limit': limit}
headers = {}

# Paginate your request using offset
has_more = True
while has_more:
	parameters = urllib.urlencode(parameter_dict)
	get_url = get_all_companies_url + parameters
	r = requests.get(url= get_url, headers = headers)
	response_dict = json.loads(r.text)
	has_more = response_dict.get('has-more') or False
	deal_list.extend(response_dict.get('deals') or [])
	parameter_dict['Offset']= response_dict.get('offset')
 # Exit pagination, based on what ever value you've set your max results variable to
	if len(deal_list) >= max_results:
		print('maximum number of results exceeded')
		break

print('loop finished')

list_length = len(company_list) 

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

```
