Airtable API for developers

Airtable is an amazing tool. Understand why it is important for any developer to understand it and its API

Airtable is an amazing tool.

It is a hybrid between spreadsheets and databases.

As a developer, you can use a very easy-to-use interface to create databases, and easy to use and edit spreadsheets, and you can easily update records even from a mobile application.

Perfect prototype

However, Airtable is more than just an excellent spreadsheet. This is a perfect tool for developers seeking to prototype or create an application MVP.

The MVP or Minimum Viable Product is an initial version of the application or product.

Most products fail because of technical limitations or "the stack cannot be expanded." They failed because either it was unnecessary or the manufacturer did not have a clear way of marketing the product.

Creating an MVP can minimize the risk of spending months trying to build the perfect application and then no one realizes it.

Great API

Airtable has an absolutely good API that can easily interface with your Airtable database programmatically.

This means that it is 10 times higher than standard spreadsheets in terms of data processing and ease of authentication.

The API has a limit of 5 requests per second. Although this request is not high, it can still be used in most cases.

Excellent documentation of the API

This is the Airtable API documentation:https://airtable.com/api.

As developers, we spend a lot of time reading documentation and trying to figure out how things work.

The API is tricky because you need to interact with the service, and you need to understand what the service exposes and how to use the API to complete the required work.

Airtable has raised the standard of all API documents. It puts the API key, base ID, and table name directly in the example, so you only need to copy and paste them into the code base.

Not only that, the examples in the API documentation use actual data in the table. In this image, notice how the field example values are the actual values I put in the table:

The Airtable API docs

API documentation provides usage examplescurl:

Examples using curl

And their official Node.js client:

Examples using Node.js

Official Node.js client

The airport remains officialAirtable.jsNode.js client library, which is a very easy-to-use way to access Airtable data.

It is convenient because it provides built-in logic to handle the rate limit and retry the request when the limit is exceeded.

Let's take a look at some common operations you can do with the API, but first let's define some values that we will reference in the code:

  • API_KEY: Airtable API key
  • BASE_NAME: The name of the base you will use
  • TABLE_NAME: The name of the table in the foundation.
  • VIEW_NAME: The name of the table view.

Base is short-termdatabase, And can contain many tables.

A table has one or more views that organize the same data in different ways. There is always at least one view (see more)

Certification

You can setAIRTABLE_API_KEYEnvironment variable, Airbase.js will automatically use this variable, or add it explicitly to your code:

const Airtable = require('airtable')

Airtable.configure({ apiKey: API_KEY })

Initialize the base

const base = require('airtable').base(BASE_NAME)

Or, if you have already initialized the Airtable variable, use

const base = Airtable.base(BASE_NAME)

Reference table

usebaseObject, you can now use

const table = base(TABLE_NAME)

Retrieval record

Any row in the table is calledrecording.

Airtable returns up to 100 records in each page of results. If you know that there will never be more than 100 items in the table, just usefirstPagemethod:

table.select({
    view: VIEW_NAME
}).firstPage((err, records) => {
    if (err) {
      console.error(err)
      return
    }
<span style="color:#75715e">//all records are in the `records` array, do something with it

})

If you have (or expect) more than 100 records, you need to useeachPagemethod:

let records = []

// called for every page of records const processPage = (partialRecords, fetchNextPage) => { records = […records, …partialRecords] fetchNextPage() }

// called when all the records have been retrieved const processRecords = (err) => { if (err) { console.error(err) return }

//process the records array and do something with it }

table.select({ view: VIEW_NAME }).eachPage(processPage, processRecords)

Check the contents of the record

Any record has many properties that can be checked.

First, you can get its ID:

record.id

//or record.getId()

And creation time:

record.createdTime

You can get any of its attributes, which can be accessed by column name:

record.get('Title')
record.get('Description')
record.get('Date')

Get a specific record

You can get a specific record by ID:

const record_id = //...

table.find(record_id, (err, record) => {
    if (err) {
      console.error(err)
      return
    }
<span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">record</span>)

})

Or by specific column value:

const getData = url => {
  table.select({
    filterByFormula: `{url} = "${url}"`
  }).eachPage(function page(records) {
    records.forEach(function(record) {
      console.dir(record.get('json'))
    })
  })
}

It's also very convenient to use.all()And async/await if you want to return data from the function:

const getData = async url => {
  const records = await table.select({
    filterByFormula: `{url} = "${url}"`
  }).all()

return records[0].get(‘json’) }

noteall()Get results of all pages synchronously, and use it only when you have a few pages of results (or 1 result, as in this example)

Create a new record

You can add new records

table.create({
  "Title": "Tutorial: create a Spreadsheet using React",
  "Link": "https://flaviocopes.com/react-spreadsheet/",
}, (err, record) => {
    if (err) {
      console.error(err)
      return
    }
<span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">record</span>.<span style="color:#a6e22e">getId</span>())

})

update record

You can use the following methods to update a single field of the record, while leaving other fields unchangedupdate:

const record_id = //...

table.update(record_id, {
  "Title": "The modified title"
}, (err, record) => {
    if (err) {
      console.error(err)
      return
    }
<span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">record</span>.<span style="color:#a6e22e">get</span>(<span style="color:#e6db74">'Title'</span>))

})

Or, you can update some fields in the record and thenClearYou haven't touchedreplace:

const record_id = //...

table.replace(record_id, {
  "Title": "The modified title",
  "Description": "Another description"
}, (err, record) => {
    if (err) {
      console.error(err)
      return
    }
<span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">record</span>)

})

Delete Record

Can use delete record

const record_id = //...

table.destroy(record_id, (err, deletedRecord) => {
    if (err) {
      console.error(err)
      return
    }
<span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">'Deleted record'</span>, <span style="color:#a6e22e">deletedRecord</span>.<span style="color:#a6e22e">id</span>)

})


More service tutorials: