Airtable: Filtering Records and Fields When Making API Calls
Back in February, Airtable introduced several new features to their API. One of the new features makes it possible to filter the records that are returned by the API, while another allows you to filter the columns that are returned.
I was recently working on an Airtable integration project, and those new features came in very handy. Let's take a look at the new features as well as a few examples of how to use them. To demonstrate the features, I'll make API calls to an Airtable database that was created using the Personal CRM template.
Getting All Records
Let's start by reviewing the URL that you would use to get all records and all fields from the Contacts table. Here's what that URL would look like:
Airtable will respond with an "INVALID_FILTER_BY_FORMULA" error. There are actually two problems with the formula.
First, the column name ("Should Reach Out") includes spaces. To resolve that problem, you surround the field name with braces. The second problem is that in order to use TRUE, you have to include parentheses.
Notice that the filter formula includes the two filters that were discussed earlier, and that they are grouped using the "AND" formula function. So only records that meet both conditions will be returned.
Similarly, we can use the "OR" function to return records that meet at least one filter condition. For example, to retrieve records that are in the "Personal" category or whose "Should Reach Out" cell has been checked, we could use this:
You've seen several examples of how you can use the "filterByFormula" parameter to filter the records that are returned by the API. You can also filter the fields that are returned using the "fields" parameter.
For example, suppose that you wanted to retrieve only the Name and Location values for all records in the Contacts table. That URL might look like this:
Of course, you can combine the "fields" parameter with the "filterByFormula" parameter. For example, to retrieve only the Name and Location of records in the "Personal" category, you would use something like this:
As you can see, the "filterByFormula" and "fields" parameters add a lot of power and flexibility to the Airtable API.
The key to making the most of the "filterByFormula" parameter is knowing what formulas Airtable supports and how to use them. If you are interested in learning more, check out the Airtable Formula Field Reference. And for more Airtable resources, click here.