🚀 Join the free Airwalker beta release

Airwalker

Back to Airwalker blog

How to use the Airtable API with Python

Get started creating Airtable API integrations using Python and pyAirtable

In this post we'll go over how we can use the Airtable REST API to perform operations on bases via pyAirtable.

Installation

pip install pyairtable

Getting authenticated

Let's list all of the records in a base.

I'll be working in a file named "example.py".

from pyairtable import Api

token = "patXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
api = Api(token)
table = api.get_table('appXXXXXXXXXXXXXX', 'tblXXXXXXXXXXXXXX')
print(table.all())

We need to authenticate our requests to Airtable, the simplest recommended way to do this is via a PAT (personal access token). Read more about why you should avoid Airtable API keys in 2023 and beyond.

Follow this link to the Airtable developer hub to create a PAT.

Give your PAT a name.

In this post we'll be reading and writing records, we'll create the PAT with the following scopes:

  • data.records:read
  • data.records:write

Next you'll need to ensure you've authorized the PAT to be used with the base(s) you'd like to use it with.

Having created a PAT, insert it to replace "patXXX..." in the preceding snippet.

Next you'll need to insert a base ID to replace "appXXXXXXXXXXXXXX", and a table ID to replace "tblXXXXXXXXXXXXXX".

You can obtain the IDs by navigating to a base of your choice and inspecting the URL.

This diagram is from the Airtable docs, you can follow this link to the Airtable docs where you can learn more about Airtable IDs.

With our PAT, base ID, and table ID now inserted into the snippet we're ready to execute the example.

python example.py

Here's the output I get when listing all the records in my base. Success!

[
  {
    "id": "recR4TdZmdhlxGdHy",
    "createdTime": "2023-04-14T10:40:45.000Z",
    "fields": {
      "Task": "Create blog post",
      "Status": "Done",
      "Date": "2023-04-14T12:02:00.000Z"
    }
  },
  {
    "id": "reckHBcB1Kz9pdmi3",
    "createdTime": "2023-04-14T12:02:59.000Z",
    "fields": {
      "Task": "Sign up to Airwalker",
      "Status": "Todo",
      "Date": "2023-04-14T12:02:00.000Z"
    }
  }
]

If you encounter any errors read the error messages and check the following:

  • PAT is valid
  • PAT has the correct scopes & access to your target Airtable base
  • Airtable base ID + table ID combination is valid

That's all you need to get started, the rest of this post will demonstrate other common operations available in the Airtable REST API via pyAirtable.

To learn more about the pyAirtable library follow this link to the documentation.

To learn more about the Airtable API follow this link to the API reference.

Get a record by record ID

from pyairtable import Api

token = (
    "patXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
api = Api(token)
table = api.get_table("appXXXXXXXXXXXXXX", "tblXXXXXXXXXXXXXX")
table.get("recXXXXXXXXXXXXXX")

Create a record

from pyairtable import Api
import datetime

token = (
    "patXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
api = Api(token)
table = api.get_table("appXXXXXXXXXXXXXX", "tblXXXXXXXXXXXXXX")
table.create(
    {
        "Task": "Learn how to create records in Airtable",
        "Status": "Done",
        "Date": datetime.datetime.now(datetime.timezone.utc).isoformat(),
    }
)

Create multiple records

from pyairtable import Api
import datetime

token = (
    "patXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
api = Api(token)
table = api.get_table("appXXXXXXXXXXXXXX", "tblXXXXXXXXXXXXXX")
table.batch_create(
    [{
        "Task": "Learn how to create multiple records in Airtable (1)",
        "Status": "Done",
        "Date": datetime.datetime.now(datetime.timezone.utc).isoformat(),
    },
    {
        "Task": "Learn how to create multiple records in Airtable (2)",
        "Status": "Done",
        "Date": datetime.datetime.now(datetime.timezone.utc).isoformat(),
    }]
)

Update a record

from pyairtable import Api

token = (
    "patXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
api = Api(token)
table = api.get_table("appXXXXXXXXXXXXXX", "tblXXXXXXXXXXXXXX")
table.update(
    "recXXXXXXXXXXXXXX",
    {
        "Task": "learn how to update records in Airtable",
        "Status": "Done",
    },
)

Update multiple records

from pyairtable import Api

token = (
    "patXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
api = Api(token)
table = api.get_table("appXXXXXXXXXXXXXX", "tblXXXXXXXXXXXXXX")
table.batch_update(
    [
        {
            "id": "recXXXXXXXXXXXXXX",
            "fields": {
                "Task": "learn how to update multiple records in Airtable (1)",
                "Status": "Done",
            },
        },
        {
            "id": "recXXXXXXXXXXXXXX",
            "fields": {
                "Task": "learn how to update multiple records in Airtable (2)",
                "Status": "Done",
            },
        },
    ],
)

Delete a record

from pyairtable import Api

token = (
    "patXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
api = Api(token)
table = api.get_table("appXXXXXXXXXXXXXX", "tblXXXXXXXXXXXXXX")
table.delete("recXXXXXXXXXXXXXX")

Delete multiple records

from pyairtable import Api

token = (
    "patXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
api = Api(token)
table = api.get_table("appXXXXXXXXXXXXXX", "tblXXXXXXXXXXXXXX")
table.batch_delete(["recXXXXXXXXXXXXXX", "recXXXXXXXXXXXXXX"])