APIs & Integrations

AbhijeetRahane
Participant

Unable to get all engagements getting error KeyError: 'has-more'

SOLVE

I am new to the api integrations and using following code to get all engagements, however getting error , KeyError: 'has-more' 

Please help ! 

 

Here's my code : 

 

import requests 
import json 
import urllib

max_results = 500
hapikey = 'hapikey'
limit = 5
engagement_list = []

parameter_dict={'hapikey':hapikey,'limit':limit}
headers = {}

#paginate your request using offset
has_more = True
while has_more:
    parameters = urllib.parse.urlencode(parameter_dict)
    get_url = get_all_engagements_url + parameters
    r = requests.get(url= get_url,headers = headers)
    response_dict = json.loads(r.text)
    pprint(response_dict)
    has_more = response_dict['has-more']
    engagement_list.extend(response_dict['results'])
    parameter_dict['offset']=response_dict['offset']
    if len(engagement_list)>= max_results:
        print('maximum number of results exceeded')
        break 

print('loop finished')
list_length = len(company_list)
 
 print("You've succesfully parsed through {} engagements records and added them to a list".format(list_length))

 

 

 

0 Upvotes
1 Accepted solution
AbhijeetRahane
Solution
Participant

Unable to get all engagements getting error KeyError: 'has-more'

SOLVE

Lol ! No one helped, had to figure out my self

 

Following code will give you all engagements in your Hubspot account: 

 

import json
import requests
hapikey = 'your hapikey' 


querystring = {"hapikey":hapikey}

headers = {
    'cache-control': "no-cache"
    }

all_engagements = []

response = requests.request("GET", url, headers=headers, params=querystring).json()

for eng in response['results']:
    all_engagements.append(eng)


hasMore = response['hasMore']
offset = response['offset']

while hasMore:

    querystring = {
        "hapikey":hapikey,
        "offset":offset
        }
    response = requests.request("GET", url, headers=headers, params=querystring).json()

    for eng in response['results']:
        all_engagements.append(eng)

    hasMore = response['hasMore']
    offset = response['offset']

json.dumps(all_engagements)

View solution in original post

0 Upvotes
4 Replies 4
AbhijeetRahane
Solution
Participant

Unable to get all engagements getting error KeyError: 'has-more'

SOLVE

Lol ! No one helped, had to figure out my self

 

Following code will give you all engagements in your Hubspot account: 

 

import json
import requests
hapikey = 'your hapikey' 


querystring = {"hapikey":hapikey}

headers = {
    'cache-control': "no-cache"
    }

all_engagements = []

response = requests.request("GET", url, headers=headers, params=querystring).json()

for eng in response['results']:
    all_engagements.append(eng)


hasMore = response['hasMore']
offset = response['offset']

while hasMore:

    querystring = {
        "hapikey":hapikey,
        "offset":offset
        }
    response = requests.request("GET", url, headers=headers, params=querystring).json()

    for eng in response['results']:
        all_engagements.append(eng)

    hasMore = response['hasMore']
    offset = response['offset']

json.dumps(all_engagements)
0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Unable to get all engagements getting error KeyError: 'has-more'

SOLVE

Hey, @AbhijeetRahane! Thank you very much for closing the loop and posting your solution here. It is greatly appreciated.

 

Have a beautiful day! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Unable to get all engagements getting error KeyError: 'has-more'

SOLVE

Hey, @AbhijeetRahane Thanks for the great question. I appreciate you adding in the details as well  

 

Our community member @taran42 was able to resolve a very similar issue in this post https://community.hubspot.com/t5/APIs-Integrations/Python-KeyError-When-Trying-to-Get-All-Companies/...

 

Best,

Jaycee 

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
AbhijeetRahane
Participant

Unable to get all engagements getting error KeyError: 'has-more'

SOLVE

Thank you for your reply 🙂 !! 

 

I  did check that post and made changes as suggested, however it still doesn't help.

I want to fetch all the records, however the result is still stuck at 250 records.  

 

Below is my code after changes, as suggested in the post : 

 

import requests
import json
import urllib
from pprint import pprint


max_results = 4000
hapikey = 'hapikey' 
limit = 500
engagement_list = []
parameter_dict = {'hapikey': hapikey, 'limit': limit}
headers = {}

# Paginate your request using offset
has_more = True
while has_more:
  parameters = urllib.parse.urlencode(parameter_dict)
  get_url = get_all_engagement_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
  engagement_list.extend(response_dict.get('results') 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(engagement_list) >= max_results:
    print('maximum number of results exceeded')
    break

  

print(len(engagement_list))    

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