How to Use Airtable as a Database
Airtable is a spreadsheet / database hybrid. If you are familiar with Airtable you will already know that it is a spreadsheet on steroids. It can also be a convenient way to access a basic database for your app. In this article I will explain how to create a simple node.js app to interact with the Airtable database API.
Step 1. Create a Base
First log in or create an Airtable user account.
A base is an individual spreadsheet / database where you can store your data. Bases are organized within workspaces. On the Airtable website, within your workspace, click on Add a base to add a new base.
Let's create a simple todo list. Open your base. Modify it so that it has three columns: todo, completed, and user. The todo field describes the todo, like: Buy milk. The completed field is a checkbox to indicate if the todo task has been done or not. The user field should identify the author of the todo, like: email@example.com. The fields 'todo' and 'user' can be 'single line text' field type. The field 'completed' should be 'checkbox' field type. No need to add any todos at this point. We will do so later, programmatically.
Step 2. Get Your API Connection Data
We want to modify the data via the api. To access the api go to https://airtable.com/api. If you are logged into your account, your base should be listed on this page.
Click on your base to open the api documentation. A unique documentation page for your selected base should open. On this page you will find the information you will need to connect your base to your app. As Airtable puts it: your Airtable base will provide its own API to create, read, update, and destroy records.
Get the following information:
- The table name.
- The base id.
- The API Key.
Step 3. Create a Node.js Project
On your computer, use a terminal to create a directory for this project.
mkdir airtable_nodejs cd airtable_nodejsbash
Initialize a Node.js project.
Create two files in your project directory:
Now install two node packages in your project: airtable and dotenv.
npm install airtable dotenvbash
AIRTABLE_API_KEY = <your airtable api key> AIRTABLE_BASE_ID = <your airtable base id> AIRTABLE_TABLE_NAME = <your airtable table name>
Inside of the index.js file, write the following code to make the connection data variables available inside the index.js file.
Require the airtable package.
In order to create a todo we will add the following function.
We will need to gather data for the todos. The interface for this app will be the command line terminal, so lets require the readline node package. Readline will allow command line input.
Since readline must wait for the user's input, it can be considered asynchronous. We should pause our app while waiting for the input. This is why we will wrap each call to readline in a promise.
By calling the promises within an asynchronous function we can 'await' each promise. This way the program will pause until each question is answered. Once the user inputs all of the data, the program will proceed to create the todo.
You can add functionality for read, update, and delete as well.
Finally call the create command to kick off the program.
Run this program by entering
node index.js in the terminal.
Get the source code for this project here.
Here linked is a simple todo app created in Next.js that utilizes Airtable's API. This app features a gui user interface, and authentication using NextAuth.
Thanks for reading. If you enjoyed this post, I invite you to explore more of my site. I write about web development, programming, and other fun stuff.