Pagination loop to access all deals v3

vauss
Member

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)

 

 

0 Upvotes
1 Reply 1
dennisedson
Community Manager

Hello @vauss !

I think the code in this stackoverflow thread might help out

Thanks,

Dennis



Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content