title: Hubspot API - how to loop to get all deals
body: <p>The loop just keeps returning the same JSON-object, but I change the offset... does anybody know what's wrong?</p>
  
  <p>I'll just write the whole function to be sure, probably not very dry.</p>
  
  <pre><code>function getDeals (api_key) {
  
    //get the right spreadsheet &amp; sheet
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Blad1');
    sheet.clear();
    sheet.appendRow(["Dealname", "Dealstage", "Close date", "Amount"]);
  
    //set up API variables
  
    var offset = 0;
    var queryParams = '&amp;limit=250&amp;offset=' + offset + '&amp;properties=dealname&amp;properties=dealstage&amp;properties=closedate&amp;properties=amount'
    var _url = 'https://api.hubapi.com/deals/v1/deal/paged?hapikey=' + api_key;
    var options = {
      "method": "GET",
      "muteHttpExceptions": true
    };
  
    //set up functions for call
    var apiCall = function(url, endpoint){
      var apiResponse = UrlFetchApp.fetch(url + endpoint,options);
      var json = JSON.parse(apiResponse);
      return json
    };
  
    var filterDeals = function (url, unfiltered_deals){
      var i = 0;
      while (unfiltered_deals.status !== "error" &amp;&amp; i &lt; (unfiltered_deals.deals).length) {
        if (unfiltered_deals.deals[i].properties.dealstage.value == "closedwon") {
          var dealname = unfiltered_deals.deals[i].properties.dealname.value;
          var dealstage = unfiltered_deals.deals[i].properties.dealstage.value;
          var closedate = new Date((unfiltered_deals.deals[i].properties.closedate.value)*1000);
          var amount = unfiltered_deals.deals[i].properties.amount.value;
          var deals = [dealname, dealstage, closedate, amount];
          // Append Hubspot data to Spreadsheet
          sheet.appendRow(deals);
        }
        i++;
       }
      if (unfiltered_deals.hasMore) {
        offset = unfiltered_deals.offset;
        var newDeals = apiCall(url, queryParams);
        filterDeals(_url, newDeals, offset, queryParams);
      } else {
        return
      }
    }
  
     //Api call to get deals  
    var dealCall = apiCall(_url, queryParams);
    var filteredDeals = filterDeals(_url, dealCall);
  
  };
  </code></pre>
  
  <p>i call this function with the API-key. </p>