APIs & Integrations

vauss
メンバー

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)

 

 

0 いいね!
4件の返信
keverett86
メンバー

Pagination loop to access all deals v3

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):

 

function hs_getallcontacts(limit = 100, hs_API_key = <<HUBSPOT KEY>>){
  var headers = {
  'Authorization' : 'Bearer ' + hs_API_key,
  'Content-Type' : 'application/json'
  };

  var params = {
  'method' : 'GET',
  'headers' : headers
  };

  var i = 0;
  var output = [];
  var next = '';

  while (next  != null){
    if (i == 0){
      var response = UrlFetchApp.fetch('https://api.hubapi.com/crm/v3/objects/contacts?' + 'limit=' + limit, params);
      var json = response.getContentText();
      var data = JSON.parse(json);
      var output = data.results;
      next = data.paging.next.link;
      i = 1
    }

    var response = UrlFetchApp.fetch(next, params);
    var json = response.getContentText();
    var data = JSON.parse(json);
    output.push(...data.results);
    try{
      next = data.paging.next.link;
    }
    catch(err){
      next = null
    };

  };

  return(output)
}
0 いいね!
kvn23
メンバー

Pagination loop to access all deals v3

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.

0 いいね!
SSekela
メンバー

Pagination loop to access all deals v3

Hi Felix,

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.)

 

Thanks for your help!

Shawn

0 いいね!
dennisedson
HubSpot製品開発チーム
HubSpot製品開発チーム

Pagination loop to access all deals v3

Hello @vauss !

I think the code in this stackoverflow thread might help out