APIs & Integrations

adimoglis
Member

Deals API 'hasMore' response (getting all deals)

SOLVE

Hello all,

In API Documentation, I see that there are two 'variebles' in the response:

  1. hasMore
  2. Offset

In the deault code provided by the documentation, I removed the 'max_results' condition.

 

    import requests
    import json
    import urllib

    #max_results = 500
    hapikey = 'demo'
    limit = 250
    deal_list = []
    get_all_deals_url = "https://api.hubapi.com/deals/v1/deal/paged?"
    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_deals_url + parameters
        r = requests.get(url= get_url, headers = headers)
        response_dict = json.loads(r.text)
        has_more = response_dict['hasMore']
        deal_list.extend(response_dict['deals'])
        parameter_dict['Offset']= response_dict['offset']
        print(response_dict['offset'])
        print(response_dict['hasMore'])


    print('loop finished')

    list_length = len(deal_list)

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

So, I expect the script to finish when 'has_more' attribute is False. But this will never happen, neither in the demo, nor in our company deals.

The API answers 'True' and the same 'Offset' for all iterations.

Is this the default behaviour?

0 Upvotes
1 Accepted solution
LeifInouye
Solution
HubSpot Alumni
HubSpot Alumni

Deals API 'hasMore' response (getting all deals)

SOLVE

Hi adimoglis! 

 

I've figured out the issue. In your code, 

parameter_dict[Offset]

needs to be:

parameter_dict[offset]

(lowercase o)

This is a documentation error on my part, that I will now correct. Thank you for bringing it to my attention!

View solution in original post

2 Replies 2
LeifInouye
Solution
HubSpot Alumni
HubSpot Alumni

Deals API 'hasMore' response (getting all deals)

SOLVE

Hi adimoglis! 

 

I've figured out the issue. In your code, 

parameter_dict[Offset]

needs to be:

parameter_dict[offset]

(lowercase o)

This is a documentation error on my part, that I will now correct. Thank you for bringing it to my attention!

LeifInouye
HubSpot Alumni
HubSpot Alumni

Deals API 'hasMore' response (getting all deals)

SOLVE

Hi adimoglis,

 

Interesting-- the intended behavior of that script is to operate even when a `max-results` is omitted. Let me look into this -- I will reach back out shortly. 

0 Upvotes