Google App Script not working after deals in pipeline go over 250


Hi everyone,

I'm using a Google App Script to log data from the deals in my Hubspot Pipeline into a Google Sheet. It was working fine, but now that I have more than 250 deals in my pipeline it stopped working, and I'm getting 429 error messages. I suspect there is something to do with the extra requests needed to get the next page of deals, after 250.

Can anyone help me?

Below is the code used to get the deals from the Deals API

function getDeals() {
   // Prepare authentication to Hubspot
   var service = getService();
   var headers = {headers: {'Authorization': 'Bearer '+ service.getAccessToken()}};
   // Prepare pagination
   // Hubspot lets you take max 250 deals per request.
   // We need to make multiple request until we get all the deals.
   var keep_going = true;
   var offset = 0;
   var deals = Array();
   while(keep_going) {
      // We’ll take three properties from the deals: the source, the stage & the amount of the deal
      var url = API_URL + "/deals/v1/deal/paged?&includeAssociations=true&properties=dealstage&properties=source&properties=amount&properties=dealname&properties=num_associated_contacts&limit=250&offset&properties=hubspot_owner_id&limit=250&offset="+offset;
      var response = UrlFetchApp.fetch(url, headers);
      var result = JSON.parse(response.getContentText());
      // Are there any more results, should we stop the pagination
      keep_going = result.hasMore;
      offset = result.offset;
      // For each deal, we take the stageId, source, amount, dealname & num_associated_contacts {
         var stageId = ("dealstage")) ? : "unknown";
         var source = ("source")) ? : "unknown";
         var amount = ("amount")) ? : 0;
         var dealname = ("dealname")) ? : "unknown";
         var hubspot_owner_id = ("hubspot_owner_id")) ? : "unknown";
         var num_associated_contacts = ("num_associated_contacts")) ? : "unknown";
   return deals;
1 Reply 1
HubSpot Moderator

Hi @Lucas_Farias_de_Mora,

If you're getting a 429 error, it means you're hitting the HubSpot API rate limit(s). You're either hitting the per-day limit (40k requests per portal per day) or the per-second limit (10 requests per portal per second). My guess would be that you're hitting the per-second limit; for situations where you have more than 250 deals, you need to make multiple requests, and since there's no delay between the requests you're making, you're exceeding 10 per second. If you're not pulling huge amounts of data, I'd recommend pulling each page synchronously.