Help

Is there a method on base('tableName') that can get the length (number of records) of the table?

Topic Labels: API
5019 4
cancel
Showing results for 
Search instead for 
Did you mean: 
Olivia_Ehrenrei
5 - Automation Enthusiast
5 - Automation Enthusiast

Is there a method on base(‘tableName’) that can get the length (number of records) of the table?

Here is part of my code:

var Airtable = require('airtable');
var base = new Airtable({ apiKey: 'myApiKey' }).base(
  'myBaseId'
);

base('tableName').


Here are the methods I see when typing:
Screen Shot 2022-01-30 at 11.42.34 AM

I also saw that someone suggested this:

const table = await input.tableAsync('Copied');
const primaryFieldId = table.fields[0].id;
const queryResult = await table.selectRecordsAsync({
  fields: [primaryFieldId],
});
const recordCount = queryResult.records.length;
console.log('recordCount', recordCount);

However, I’m getting an error saying: SyntaxError: await is only valid in async function (also, I wasn’t sure if input was a variable that was set somewhere else).

My other idea is to GET all of the records and implement a counter, but I wanted to see if there was a cleaner or better way.

4 Replies 4

It looks like you are using the REST API. The “other” code you are citing is for Scripting app. The REST API and Scripting work differently, and you cannot use the same code in both.

Getting the total number of of records in a table via the REST API is problematic because if you use only the REST API, you must request all of the records to get a count, and you are limited to requesting 100 records at a time with a rate limit of 5 requests per second.

There are several other techniques to get the number of records, using different combinations of scripting, automations, and base design.

That’s helpful! I’m using the API in a react app – how would recommend the best way to get the count is for this situation?

A script automation using the SDK is able to determine the number of records with precision in fewer than 100ms. It also has the capacity to call out to a webhook service to report the count. Lastly, script automations can be triggered by the addition or deletion of records in the table, thus making it possible to report the latest count in real-time and only when the count changes.

I typically use this approach and avoid the API for the reasons @kuovonne cited.

Since your react app won’t be able to receive a webhook, your options are limited. You can use a two phase approach. Have your react app call an Airtable webhook automation that triggers an action script. Have that action script calculate the number of records and write the result to a table/record. Have your react app wait for the automation script to run, and then read the result from the table.

This can be a bit klunky, and you may need to make adjustments to ensure that you get the value after the automation runs, as automation runs are not instant. For example, you could timestamp record holding the count, and then your react app can decide if the value is recent enough.

You may also have issues if you have many people using this app. You could exceed the number of automation runs per month.