Feb 13, 2022 05:37 PM
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
?
Solved! Go to Solution.
Feb 14, 2022 05:14 PM
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));
}
Feb 14, 2022 05:14 PM
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));
}