Airtable Function: Records Counter
Does anyone have prewritten JavaScript code for a function that sends a GET request to Airtable, counts all fetched records, and outputs the number to an output variable without performing any other actions? I am gonna add some parameters for sure.
1 Reply
after some debugging, here is the function:
const config = { method: 'GET', headers: { 'Authorization':
const baseId = "your_base_id";
const tableIdOrName = "your_table_id";
const AirtableToken = "airtable_token";
const url =
https://api.airtable.com/v0/${encodeURIComponent(baseId)}/${encodeURIComponent(tableIdOrName)}`;
const config = { method: 'GET', headers: { 'Authorization':
Bearer ${AirtableToken}
}
};
try {
const response = await fetch(url, config);
if (!response.ok) {
throw new Error(HTTP error! status: ${response.status}
);
}
// Voiceflow automatically parses JSON, so use .json (property) not .json()
const data = response.json;
if (!data.records || !Array.isArray(data.records)) {
throw new Error("Invalid or missing 'records' in Airtable response");
}
const records_count = data.records.length;
return {
outputVars: { records_count },
next: { path: 'success' }
};
} catch (error) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Error: " + error.message + " URL: " + url } }]
};
}
`