Jan 4, 202111:54 AM - edited Jan 4, 202111:58 AM
Member
Pagination loop to access all deals v3
Hi there,
very new to APIs so apologies if this is very simple - alas I can't figure it out.
Goal: I want to return all deals (currently –4000) to use in a forecasting model outside of Hubspot.
I use the deals endpoint in v3. I understand that each batch is limited to 100 entries and returns the next url as the 'paging' part of the response. What I don't understand is how to set up a pagination loop that uses the next url until all deals have been retrieved.
Below is what I have so far. Can anybody help?
Thanks a lot in advance,
Felix
import requests
import config #Holds my hapikey
hapikey = config.hapikey
url = f"https://api.hubapi.com/crm/v3/objects/deals?archived=false&paginateAssociations=false&limit=100&hapikey={hapikey}"
headers = {'accept': 'application/json'}
response = requests.request("GET", url, headers=headers).json()
deals = []
data = response['results']
for i in data:
deals.append(data)
for/ while ??? #What's the correct pagination loop here?
response = requests.request("GET", url, headers=headers).json()
link = response['paging']['next']['link']
url = f"{link}&hapikey={hapikey}"
data = response['results']
for i in data:
deals.append(data)
So I don't know if this was answered, but I found a simple solution. The parameter is "after" that will look for deals with ids after the give id. You can use the "next" property in the reponse variable to get the url for the next batch of deals automatically. Here is an example of of appscript for contacts (Same situation):
Hey Felix, have you been able to get your above python code to work? I'm looking to do something similiar (loop through the pages) with all contacts. The one shown in the hubspots docs (using urllib) isn't working and this way seems more straight forward if it were to work.
Not sure if you're still working with the Deal API in Hubspot. I myself am new to the API world. I too am looking for the same solution you are. I reviewed the stack overflow thread that was posted back to you. The code makes a lot of sense. I was thinking about using it However - that piece of code is NOT using the v3 API. Has anyone worked out a paging solution using the newer v3 API version? (The v3 version is what I am starting with.)