Implementing formula-based filtering in Airtable API using Google Apps Script

I’ve been working with Airtable’s API through Google Apps Script and can successfully pull data using predefined views. However, I’m struggling to figure out how to apply custom filters using the filterByFormula parameter instead of relying on views.

Here’s my current working code that fetches records from a view:

function getData() {
    const apiToken = MY_TOKEN;
    const baseId = MY_BASE;
    
    const endpoint = `https://api.airtable.com/v0/${baseId}/Orders?api_key=${apiToken}&maxRecords=5&view=Pending%20Orders`;
    
    const apiResponse = UrlFetchApp.fetch(endpoint);
    const parsedData = JSON.parse(apiResponse.getContentText());
    const recordsList = parsedData["records"];
    
    console.log(recordsList);
}

I want to replace the view parameter with a formula filter but I’m not sure about the correct syntax. Any guidance would be helpful!

yeah, same thing happened to me last month! just use encodeURIComponent() in javascript - it handles the url encoding for u. like &filterByFormula=${encodeURIComponent("Status='Pending'")}. way easier than memorizing all those encoding rules.

The filterByFormula syntax is tricky at first, but it’s really powerful once you get it. Just swap out the view parameter for something like filterByFormula=Status='Pending' or whatever condition you need.

URL encoding tripped me up when I started - make sure you encode everything properly. Spaces become %20, equals signs might need encoding, and wrap text values in single quotes.

Here’s what a status filter looks like: https://api.airtable.com/v0/${baseId}/Orders?api_key=${apiToken}&maxRecords=5&filterByFormula=Status%3D%27Pending%27

You can get fancy with AND() or OR() functions for multiple conditions. It uses Airtable’s standard formula language, so test your formulas directly in Airtable before putting them in your script.