Using Airtable as backend service
Tags: airtable, backend, spa
Published: 2020-11-01 09:42:36

---
title: Using Airtable as backend service
published: true
description: 
tags: airtable, backend, SPA
---
## The hell of finding the right backend

When it's time to write a Single Page Application in javascript, we often need some sort of backend so that the site owne can add or update the content in the application.

There is many possible alternative ways, from building a complexe REST application on dedicated framework such as Laravel or Ruby on Rails, to connecting your app to headless CMS.

The problem with headless is that are most of the time costly and can be expersive after several years. Or you have to install and manage it on a virtual private server with all the security issues that comes with.

## Airtable Solution
One quick and easy solution is to use [Airtable](https://airtable.com/). It comes with a free plan, REST API, and great documention.

It's take a minute to create a table, less to alter columns, no migration is needed and it's super easy for the site owner to add the content. Think Airtable as a combination of Excel and Access, without the code and the hugly.

It come with his own [Univers](https://airtable.com/universe) and you'll probably find a template for your project.

Nevertheless Airtable has a major gotcha that limit his usability for heavy applications.

## Exemple

Here is an [Airtable](https://airtable.com/invite/l?inviteId=invM2yvDAcUIvCGZn&inviteToken=296e5808079128161543fe975d7cbccf9cd914cd4bfa6fc0c45429388625ec9a) base for building a Quiz with questions and answers. It comes with an awesome base specific [API documentation](https://airtable.com/api) (you must be logged in to see it) and an official [Javascript library](https://github.com/Airtable/airtable.js).

### Authentification 

Authentification is straightforward:

```javascript
const Airtable = require("airtable");
const base = new Airtable({ apiKey: "YOUR_API_KEY" }).base(
  "BASE_TOKEN"
);
```

### Fetching single record

Here is how you can fetch a record with his ID (ID is created autonumber field):

```javascript
fetchQuiz(id) {
      return base("Questions")
        .select({
          filterByFormula: `{ID} = ${id}`,
        })
        .firstPage()
        .catch(function (err) {
          if (err) {
            console.error(err);
            return;
          }
        });
    }
```

The `filterByFormula` key is an Airtable [Formula](https://support.airtable.com/hc/en-us/articles/203255215-Formula-Field-Reference) instruction that offers SQL like queries.

### Fetching multiple records

```javascript
base('Questions').select({
    // Selecting the first 20 records in Grid view.
    // You can also create you own view.
    maxRecords: 20,
    view: "Grid view"
}).eachPage(function page(records, fetchNextPage) {
    // This function (`page`) will get called for each page of records.

    records.forEach(function(record) {
        console.log('Retrieved', record.get('Question'));
    });

    // To fetch the next page of records, call `fetchNextPage`.
    // If there are more records, `page` will get called again.
    // If there are no more records, `done` will get called.
    fetchNextPage();

}, function done(err) {
    if (err) { console.error(err); return; }
});
```

### Creating

Creating a single record is quite simple:

```Javascript
base('Questions').create({
  "Question": "In vuejs, which directive is to used to attach event listeners that invoke methods?",
}, function(err, record) {
  if (err) {
    console.error(err);
    return;
  }
  console.log(record.getId());
});
```

The Airtable javascript library offers also functions to create multiple records, update and delete records.

## Gotchas

The API is limited to 5 requests per second... If you exceed this rate, you will receive an error and will need to wait 30 seconds before subsequent requests will succeed.

So it definitely limit the use of Airtable for extensive application. But it can find a place for prototyping or building small application. For instance, at [Hawaii Interactive](https://hawaii.do/) we used it to quickly build a swiss [quiz on energy](https://quiz.explorateurs-energie.ch/) for schoolchildren.