Sorting and Filtering Query Results in Airtable

Sorting and Filtering Query Results in Airtable

With the new APIs & Services feature, you’re able to save the base URL for any REST API which will allow you to easily reuse and modify your request. We’ll go over an example of this with sorting and filtering Airtable query results.

The Base

image

For this example, we’ll be using a simple Base with two Tables that was created to keep track of favorite album releases.

The API

Once your Base is ready, you can go to Airtable’s API Documentation found under the ‘Help’ button in the top right corner of your Base.

image

Make sure you check the ‘show API key’ checkbox to generate your key — we’ll save this in the Global Variables settings back in Draftbit.

For previous Airtable tutorials, we’ve had you copy and paste the entire URL in the example generated, but for this one, we’re going to leave out the 2019 (the Table Name) at the end.

image

If you go further down to the List Records section of the documentation, you’ll see a section discussing the ways you can filter and sort your query. Keep this open, we’ll come back to this later.

image

In Draftbit

To configure this in the APIs and Services, open the Project Settings by clicking the gear in the column on the left of the Builder and go to the Variables tab. Give the new variable a name (Key), paste your API Key in the Value section and click the ‘Add’ button.

image

Next, click the Data button on the left-hand side of the builder and select Rest API under the Add a Service header. Here is where you will:

  1. Give your Service a Name
  2. Paste the Base URL into the Base URL input (make sure there's no / at the end)
  3. Add your API key — Create a new Header, make sure its key is 'Authorization' and select your Global Variable from the Value dropdown.
image

Next, you can create the endpoint for your query. Airtable has made it super easy to generate the proper URL to filter and sort your results with its API Encoding Tool (If you access this tool directly from the Airtable API documentation, it’ll have the Base ID and Table fields pre-populated for you). Once you’ve filled in the fields you need, you can copy the portion of the URL after the Base ID to use in Draftbit.

Here’s an example to generate a URL that will return a list of Albums in the 2020 Table sorted by the release month in ascending order.

image

Once you’re back in Draftbit, create a new endpoint in your Airtable service and paste the necessary portion of the URL into the Add Path & Params input. Test the request and you’ll see the results of your sort/filter appear!

image

Make sure to refer back to Airtable’s API documentation to see the full list of query parameters you can use in your own requests!