Python code to get all objects.

VDrigin
Participant

 I would like to use python to access all objects/SOME_OBJECT_TYPE.

I am trying to use this peace of coding. However, it limits results to 100.

 

url = "https://api.hubapi.com/crm/v3/objects/2-1110315/?properties=introduction_date&properties=lender_name..."

querystring = {"limit":"100","archived":"false","hapikey":"HUBSPOT KEY"}

headers = {'accept': 'application/json'}

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

 

Previously, I have been using this chunk of code. 

For example, all deals could be obtain using this:

 

#%%
max_results = 1000
hapikey = 'HUBSPOT API'
limit = 250
deal_list = []
get_all_deals_url = "https://api.hubapi.com/deals/v1/deal/paged?&properties=dealname&properties=dealstage&properties=note..."
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']
if len(deal_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(deal_list)

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

 

However the structure of response_dict = json.loads(r.text) is changed. It does not have the keys: 'hasMore' and 'offset' anymore.

Now it has the key "paging" instead.

Could you help to write the code that extracts all the objects as for deals above?

Thanks a lot!

0 Upvotes
2 Replies 2
dennisedson
Community Manager

@BJacobson , would you be able to help out here?

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
webdew
Top Contributor | Diamond Partner

Hi @VDrigin ,

Hope below code help :

Note : incluce requests in python :

import requests

url = "https://api.hubapi.com/crm/v3/objects/p<PORTAL_ID>_<object_KEY>?&hapikey=demo"

querystring = {"hapikey":"YOUR_HUBSPOT_API_KEY"}

payload = "{\"properties\":{\"<property_key1>\":\"<property_value2>\",\"<property_key2>\":\"<property_value2>\"}}"
headers = {
'accept': "application/json",
'content-type': "application/json"
}

response = requests.request("PATCH", url, data=payload, headers=headers, params=querystring)

print(response.text)


Hope this helps!


If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards. 

0 Upvotes