Help

Simple fetchNextPage example in google app script

Topic Labels: API
Solved
Jump to Solution
1605 1
cancel
Showing results for 
Search instead for 
Did you mean: 
David_Weinstein
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi. Is there a very simple example of how to consume multiple pages when calling GET on the airtable API? I have a simple API Call class, much of which I got from this forum, but there are multiple pages in my data.

Here’s my code:

class AirtableGet{
  constructor(inputSheetName, tableName){
    this.inputSheetName = inputSheetName; 
    this.tableName = tableName; // text name of the table in Airtable; ex. "Balance Tracking"

    /** Airtable Variables */
    this.ss = SpreadsheetApp.getActiveSpreadsheet();
    this.inputsSheet = this.ss.getSheetByName(this.inputSheetName);
    this.myAPIKey = this.inputsSheet.getRange(9, 2).getValue();
    this.baseKey = this.inputsSheet.getRange(10, 2).getValue();
    this.cAirtableAPIEndpoint = this.inputsSheet.getRange(11, 2).getValue();
    this.url = this.cAirtableAPIEndpoint + this.baseKey + "/" + encodeURIComponent(this.tableName);
  };// end constructor

  /** get request */
  getFromAirtable (){
      var options =
          {
            method: "GET",
            headers: {
              'Authorization': 'Bearer ' + this.myAPIKey,
              'Content-Type': 'application/json'
              
            },
            // payload: JSON.stringify(this.payloadFrame),
            muteHttpExceptions: true,
            followRedirects: true,
          };
      var fRawResponse = UrlFetchApp.fetch(this.cAirtableAPIEndpoint + this.baseKey + "/" + encodeURIComponent(this.tableName) + "?maxRecords=500", options);
      var jsonTxs = JSON.parse(fRawResponse)
      console.log(jsonTxs.records.length); // how do I get the page size response?
      // return response
  };
}; 

Where/how do I call fetchNextPage?

1 Solution

Accepted Solutions
Bill_French
17 - Neptune
17 - Neptune

This doesn’t reveal all of the secret sauce in my integration approach but this should give you a pathway that works.

//
// get airtable records
//
function atGetTable_(baseID, tableName)
{
  const options = {
    method: 'GET',
    headers: {
      'Authorization' : 'Bearer ' + cMyAirtableAPIKey,
      'Content-type': 'application/json'
    },
    muteHttpExceptions : true
  };
  
  var offSet = true;
  var currentPage = 0;
  var aRecords = [];
  var offsetParameter = "";
  while (offSet)
  {
    var response = UrlFetchApp.fetch(cAirtableAPIEndpoint + baseID + "/" + encodeURIComponent(tableName) + "?pageSize=100" + offsetParameter, options).getContentText();
    aRecords = aRecords.concat(JSON.parse(response).records);
    var offSet = JSON.parse(response).offset;
    if (offSet)
    {
      offsetParameter = "&offset=" + encodeURIComponent(offSet);
    } else {
      break;
    }
    currentPage += 1;
  }
  
  var oRecords = {
    "records" : aRecords
  }
  
  return(JSON.stringify(oRecords));
  
}

See Solution in Thread

1 Reply 1
Bill_French
17 - Neptune
17 - Neptune

This doesn’t reveal all of the secret sauce in my integration approach but this should give you a pathway that works.

//
// get airtable records
//
function atGetTable_(baseID, tableName)
{
  const options = {
    method: 'GET',
    headers: {
      'Authorization' : 'Bearer ' + cMyAirtableAPIKey,
      'Content-type': 'application/json'
    },
    muteHttpExceptions : true
  };
  
  var offSet = true;
  var currentPage = 0;
  var aRecords = [];
  var offsetParameter = "";
  while (offSet)
  {
    var response = UrlFetchApp.fetch(cAirtableAPIEndpoint + baseID + "/" + encodeURIComponent(tableName) + "?pageSize=100" + offsetParameter, options).getContentText();
    aRecords = aRecords.concat(JSON.parse(response).records);
    var offSet = JSON.parse(response).offset;
    if (offSet)
    {
      offsetParameter = "&offset=" + encodeURIComponent(offSet);
    } else {
      break;
    }
    currentPage += 1;
  }
  
  var oRecords = {
    "records" : aRecords
  }
  
  return(JSON.stringify(oRecords));
  
}