In order to use this library, you need to create an account on airtable, free accounts provide sufficient rights to use the API and are good enough to get started.
Rate limit
The API is limited to 5 requests per second per base. If you exceed this rate, you will receive a 429 status code and will need to wait 30 seconds before subsequent requests will succeed.
IMPORTANT
Once you have created your first spreadsheet, you will need the spreadsheet API ID and the table name.
To find them:
- go to https://airtable.com/api
- click on the speadsheet you would like to use
- on the spreadsheet API page, click on the table you would like to work on (on the left panel)
- you will find the spreadsheet API ID and the table name in the url as follow: https://airtable.com/{API_SPREADSHEET_ID}/api/docs#curl/table:{TABLE_NAME}.
do records = App(
"airtable",
spreadsheet_id="MY_SPREADSHEET_ID",
table_name="AN_AWESOME_TABLE",
method="getRecords"
)
Usage
The target spreadsheet ID and table name must be passed to all functions.
Create a new record
A record on airtable is similar to a row in excel. To create a new record, simply pass to the function an object with the fields and values as follows:
- You can select which fields you would like with the
fields
parameter. - Formulas are to be passed in the
filterByFormula
parameter. Here we get all the rows that have a fieldjob
not empty:
do fields = {
"firstname": "Bastien",
"email": "bastien@csml.dev",
"job": "COO"
}
do newRecord = App(
"airtable",
method="createRecord",
spreadsheet_id="MY_SPREADSHEET_ID",
table_name="AN_AWESOME_TABLE",
fields=fields
)
Create several new records
do records = [
{
"firstname": "Bastien",
"email": "bastien@csml.dev",
"job": "COO"
}, {
"firstname": "François",
"email": "francois@csml.dev",
"job": "CTO"
}
]
do newRecords = App(
"airtable",
method="createRecords",
spreadsheet_id="MY_SPREADSHEET_ID",
table_name="AN_AWESOME_TABLE",
records=records
)
Get records
You can get an array of records from Airtable, and you can also pass a query to filter the rows.
More information about Airtable formulas: https://support.airtable.com/hc/en-us/articles/203255215-Formula-Field-Reference
do fields = ["firstname", "email"]
do filterByFormula = "NOT({job} = '')"
do sort = {"field": "firstname", "direction": "asc"}
do records = App(
"airtable",
method="getRecords",
spreadsheet_id="MY_SPREADSHEET_ID",
table_name="AN_AWESOME_TABLE",
fields=fields, // optional
sort=sort, // optional
filterByFormula=filterByFormula, // optional
)
Update a record
You can update a record by sending the updated version of the specific record returned by the getRecords method.
This updated object should include the Airtable internal record id that you'll get with the getRecords method.
// Let's use the `records` variable set above and edit the first record of the array
do record = records[0]
do record.firstname = "François"
do records = App(
"airtable",
method="updateRecord",
spreadsheet_id="MY_SPREADSHEET_ID",
table_name="AN_AWESOME_TABLE",
record=record
)