Airtable
Overview
The Airtable class allows you to interact with an Airtable base. In order to use this class you must generate an Airtable API Key which can be found in your Airtable account settings.
Note
- Finding The Base Key
The easiest place to find the
base_key
for the base that you wish to interact with is via the Airtable API documentation.Go to the Airtable API Base List and select the base.
The url of the resulting page will contain the
base_key
.Example:
https://airtable.com/[BASE_KEY]/api/docs#curl/introduction
QuickStart
To instantiate the Airtable class, you can either store your Airtable API
AIRTABLE_API_KEY
as an environmental variable or pass in your api key
as an argument. You also need to pass in the base key and table name.
from parsons import Airtable
# First approach: Use API credentials via environmental variables and pass
# the base key and the table as arguments.
at = Airtable(base_key, 'table01')
# Second approach: Pass API credentials, base key and table name as arguments.
at = Airtable(base_key, 'table01', api_key='MYFAKEKEY')
You can then call various endpoints:
# Get records from a base
at.get_records(fields=['id', 'fn', 'ln'])
# Get a single record from a base
at.get_record(1233)
# Insert records
tbl.from_csv('my_new_records')
at.insert_records(tbl)
API
- class parsons.Airtable(base_key, table_name, api_key=None)[source]
- Args:
- base_key: str
The key of the Airtable base that you will interact with.
- table_name: str
The name of the table in the base. The table name is the equivilant of the sheet name in Excel or GoogleDocs.
- api_key: str
The Airtable provided api key. Not required if
AIRTABLE_API_KEY
env variable set.
- get_record(record_id)[source]
Returns a single record.
- Args:
- record_id: str
The Airtable record id
- Returns:
A dictionary of the record
- get_records(fields=None, max_records=None, view=None, formula=None, sort=None, sample_size=None)[source]
- Args:
- fields: str or lst
Only return specified column or list of columns. The column name is case sensitive
- max_records: int
The maximum total number of records that will be returned.
- view: str
If set, only the records in that view will be returned. The records will be sorted according to the order of the view.
- formula: str
The formula will be evaluated for each record, and if the result is not 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:"NOT({COLUMN_A}='')"
For more information see Airtable Docs on formulas.
Usage - Text Column is not empty:
airtable.get_all(formula="NOT({COLUMN_A}='')")
Usage - Text Column contains:
airtable.get_all(formula="FIND('SomeSubText', {COLUMN_STR})=1")
- sort: str or lst
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 -.
Example usage:
airtable.get_records(sort=['ColumnA', '-ColumnB'])
- sample_size: int
Number of rows to sample before determining columns
- Returns:
- Parsons Table
See Parsons Table for output options.
- insert_record(row)[source]
Insert a single record into an Airtable.
- Args:
- row: dict
Fields to insert. Must be dictionary with Column names as Key.
- typecast: boolean
Automatic data conversion from string values.
- Returns:
Dictionary of inserted row
- insert_records(table, typecast=False)[source]
Insert multiple records into an Airtable. The columns in your Parsons table must exist in the Airtable. The method will attempt to map based on column name, so the order of the columns is irrelevant.
- Args:
- table: A Parsons Table
Insert a Parsons table
- typecast: boolean
Automatic data conversion from string values.
- Returns:
List of dictionaries of inserted rows
- update_record(record_id, fields, typecast=False)[source]
Updates a record by its record id. Only Fields passed are updated, the rest are left as is.
- Args:
- record_id: str
The Airtable record id
- fields: dict
Fields to insert. Must be dictionary with Column names as Key.
- typecast: boolean
Automatic data conversion from string values.
- Returns:
None