Working with Tables

Note

Airtable imposes a rate limit of 5 QPS per base. If you exceed that limit, their API will return 429 errors for a moment. By default, pyAirtable will retry 429 errors up to five times, but retrieving many pages of records might take several seconds. Read more at pyairtable.retry_strategy().

Supported Endpoints

The Airtable API exposes a number of endpoints for manipulating data within tables. The grid below maps Airtable’s official API endpoints to this library’s methods.

Airtable Endpoint

pyAirtable Method

Get a record

get()

Get all records

all()

Get matching records

all(formula=...)

Get first match

first()

Create a record

create()

Update a record

update()

Replace a record

update(replace=True)

Delete a record

delete()

Create multiple records

batch_create()

Update multiple records

batch_update()

Upsert multiple records

batch_upsert()

Delete multiple records

batch_delete()

Fetching Records

iterate()

Iterate over a set of records of size page_size, up until max_records or end of table.

>>> for records in table.iterate(page_size=100, max_records=1000):
...     print(records)
...
[{'id': 'rec123asa23', 'fields': {'Last Name': 'Alfred', 'Age': 84}, ...}, ...]
[{'id': 'rec123asa23', 'fields': {'Last Name': 'Jameson', 'Age': 42}, ...}, ...]

all()

This method returns a single list with all records in a table. Note that under the hood it uses iterate() to fetch records so it might make multiple requests.

>>> table.all(sort=["Name", "-Age"])
[{'id': 'rec123asa23', 'fields': {'Last Name': 'Alfred', 'Age': 84}, ...}, ...]

Parameters

Airtable’s API offers a variety of options to control how you fetch data.

Most options in the Airtable API (e.g. sort, fields, etc.) have a corresponding keyword argument that can be used with fetching methods like iterate() or all().

Keyword Argument

Notes

max_records

The maximum total number of records that will be returned. If this value is larger than page_size, multiple requests will be needed to fetch all records.

sort

List of fields to sort by. Default order is ascending. This parameter specifies how the records will be ordered. If you set the view parameter, the returned records in that view will be sorted by these fields. If sorting by multiple columns, column names can be passed as a list. Sorting Direction is ascending by default, but can be reversed by prefixing the column name with a minus sign -.

view

The name or ID of a view. If set, only the records in that view will be returned. The records will be sorted according to the order of the view.

page_size

The number of records returned in each request. Must be less than or equal to 100. If no value given, Airtable’s default is 100.

formula

An Airtable formula. The formula will be evaluated for each record, and if the result is none of 0, false, "", NaN, [], or #Error! the record will be included in the response. If combined with view, only records in that view which satisfy the formula will be returned. For example, to only include records where COLUMN_A isn’t empty, pass in formula="{COLUMN_A}".

fields

Name of field or fields to be retrieved. Default is all fields. Only data for fields whose names are in this list will be included in the records. If you don’t need every field, you can use this parameter to reduce the amount of data transferred.

cell_format

The cell format to request from the Airtable API. Supported options are json (the default) and string. json will return cells as a JSON object. string will return the cell as a string. user_locale and time_zone must be set when using string.

user_locale

The user locale that should be used to format dates when using string as the cell_format. See https://support.airtable.com/hc/en-us/articles/220340268-Supported-locale-modifiers-for-SET-LOCALE for valid values.

time_zone

The time zone that should be used to format dates when using string as the cell_format. See https://support.airtable.com/hc/en-us/articles/216141558-Supported-timezones-for-SET-TIMEZONE for valid values.

return_fields_by_field_id

New in version 1.3.0.

An optional boolean value that lets you return field objects where the key is the field id. This defaults to false, which returns field objects where the key is the field name.

Return Values

This library will return records as RecordDict.

>>> table.all()
[
    {
        'id': 'recwPQIfs4wKPyc9D',
        'createdTime': '2017-03-14T22:04:31.000Z'
        'fields': {
            'Name': 'Alice',
        },
    },
    {
        'id': 'rechOLltN9SpPHq5o',
        'createdTime': '2017-03-20T15:21:50.000Z'
        'fields': {
            'Name': 'Bob',
        },
    },
    {
        'id': 'rec5eR7IzKSAOBHCz',
        'createdTime': '2017-08-05T21:47:52.000Z'
        'fields': {
            'Name': 'Carol',
        },
    }
]

Formulas

The pyairtable.formulas module provides functionality to help you compose Airtable formulas.

  • match() checks field values from a Python dict:

    >>> from pyairtable.formulas import match
    >>> formula = match({"First Name": "John", "Age": 21})
    >>> formula
    "AND({First Name}='John',{Age}=21)"
    >>> table.first(formula=formula)
    {"id": "recUwKa6lbNSMsetH", "fields": {"First Name": "John", "Age": 21}}
    
  • to_airtable_value() converts a Python value to an expression that can be included in a formula:

    >>> from pyairtable.formulas import to_airtable_value
    >>> to_airtable_value(1)
    1
    >>> to_airtable_value(datetime.date.today())
    '2023-06-13'
    

For more on generating formulas, look over the pyairtable.formulas API reference.

Retries

As of 2.0.0, the default behavior is to retry requests up to five times if the Airtable API responds with a 429 status code, indicating you’ve exceeded their per-base QPS limit. To adjust the default behavior, you can use the retry_strategy() function.

Creating Records

create()

Creates a single record from a dictionary representing the table’s fields.

>>> table.create({'Name': 'John'})
{'id': 'rec123asa23', 'fields': {'Name': 'John', ...}}

batch_create()

Create multiple records from a list of WritableFields dicts.

>>> table.batch_create([{'Name': 'John'}, ...])
[{'id': 'rec123asa23', 'fields': {'Name': 'John'}}, ...]

Updating Records

update()

Updates a single record for the provided record_id using a dictionary representing the table’s fields.

>>> table.update('recwPQIfs4wKPyc9D', {"Age": 21})
[{'id': 'recwPQIfs4wKPyc9D', 'fields': {"Name": "John", "Age": 21}}, ...]

batch_update()

Update multiple records from a list of UpdateRecordDict.

>>> table.batch_update([{"id": "recwPQIfs4wKPyc9D", "fields": {"Name": "Matt"}}, ...])
[{'id': 'recwPQIfs4wKPyc9D', 'fields': {"Name": "Matt", ...}}, ...]

batch_upsert()

New in version 1.5.0.

Batch upsert (create or update) records from a list of records. For details on the behavior of this Airtable API endpoint, see Update multiple records.

>>> table.batch_upsert(
...     [{"id": "recwPQIfs4wKPyc9D", "fields": {"Name": "Matt"}}, ...],
...     key_fields=["Name"]
... )
[{'id': 'recwPQIfs4wKPyc9D', 'fields': {'Name': 'Matt', ...}}, ...]

Deleting Records

delete()

Deletes a single record using the provided record_id.

>>> table.delete('recwPQIfs4wKPyc9D')
{'deleted': True, 'id': 'recwPQIfs4wKPyc9D'}

batch_delete()

Batch delete records using a list of record ids.

>>> table.batch_delete(['recwPQIfs4wKPyc9D', 'recwAcQdqwe21asdf'])
[{'deleted': True, 'id': 'recwPQIfs4wKPyc9D'},
 {'deleted': True, 'id': 'recwAcQdqwe21asdf'}]

Commenting on Records

pyAirtable allows you to access, create, and modify comments on records through the Table class. Both the comments() and add_comment() methods will return instances of Comment, which can be modified, saved, or deleted.

>>> table = Api.table("appNxslc6jG0XedVM", "tblslc6jG0XedVMNx")
>>> comment = table.add_comment("recMNxslc6jG0XedV", "Hello, @[usrVMNxslc6jG0Xed]!")
>>> table.comments("recMNxslc6jG0XedV")
[
    Comment(
        id='comdVMNxslc6jG0Xe',
        text='Hello, @[usrVMNxslc6jG0Xed]!',
        created_time='2023-06-07T17:46:24.435891',
        last_updated_time=None,
        mentioned={
            'usrVMNxslc6jG0Xed': Mentioned(
                display_name='Alice',
                email='alice@example.com',
                id='usrVMNxslc6jG0Xed',
                type='user'
            )
        },
        author=Collaborator(
            id='usr0000pyairtable',
            email='pyairtable@example.com',
            name='Your pyairtable access token'
        )
    )
]
>>> comment.text = "Never mind!"
>>> comment.save()
>>> table.comments("recMNxslc6jG0XedV")[0].text
'Never mind!'
>>> comment.delete()