Python Client Library for Airtable
The `airtable` library, maintained by josephbestjames on PyPI (version 0.4.8), is a Python client for interacting with the Airtable REST API. It provides a straightforward interface for performing CRUD (Create, Read, Update, Delete) operations on Airtable bases and tables, abstracting the underlying HTTP requests. This library has a relatively slow release cadence, with its latest update in early 2021. Developers seeking a more actively maintained and feature-rich client should consider `pyairtable`.
Warnings
- breaking This `airtable` library (josephbestjames/airtable.py) is distinct from `pyairtable` (gtalarico/pyairtable). The `pyairtable` library is the actively maintained successor, offering more features, better documentation, and more frequent updates. Many online resources and migration guides refer to `pyairtable`. Mixing up these libraries will lead to `ModuleNotFoundError` or unexpected API behavior.
- breaking Airtable deprecated legacy API keys on February 1, 2024. While this `airtable` library might still accept API keys, it is strongly recommended to switch to Personal Access Tokens (PATs) for authentication. Using deprecated API keys may lead to authentication failures in the future.
- gotcha Naming your Python script `airtable.py` will cause a `ModuleNotFoundError` when you try to import `airtable`. This happens because Python's import system will prioritize your local file over the installed package.
- gotcha Airtable's API enforces a rate limit of 5 requests per second per base. Exceeding this limit will result in 429 'Too Many Requests' errors.
- deprecated Some older tutorials or documentation for this library (or its predecessors/forks) might refer to methods like `get_all()` or `insert()`. The current version of this `airtable` library (0.4.8) primarily uses `get()` for fetching records (optionally with an ID for a single record) and `create()` for inserting new records.
Install
-
pip install airtable
Imports
- Airtable
from airtable import Airtable
Quickstart
import os
from airtable import Airtable
# It's recommended to use Personal Access Tokens (PATs) and store them as environment variables.
# Legacy API keys are deprecated by Airtable as of Feb 2024.
AIRTABLE_BASE_ID = os.environ.get('AIRTABLE_BASE_ID', 'YOUR_BASE_ID')
AIRTABLE_API_KEY = os.environ.get('AIRTABLE_API_KEY', 'YOUR_PERSONAL_ACCESS_TOKEN') # Or legacy API key if absolutely necessary
AIRTABLE_TABLE_NAME = 'MyTable'
try:
at = Airtable(AIRTABLE_BASE_ID, AIRTABLE_TABLE_NAME, AIRTABLE_API_KEY)
# Fetch all records from the table
records = at.get(AIRTABLE_TABLE_NAME)
print(f"Successfully fetched {len(records['records'])} records from '{AIRTABLE_TABLE_NAME}':")
for record in records['records']:
print(f"- ID: {record.get('id')}, Fields: {record.get('fields')}")
# Example: Create a new record
new_record_data = {'Name': 'New Task', 'Status': 'To Do'}
created_record = at.create(AIRTABLE_TABLE_NAME, new_record_data)
print(f"\nCreated new record: {created_record['id']} with fields {created_record['fields']}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure AIRTABLE_BASE_ID, AIRTABLE_API_KEY, and AIRTABLE_TABLE_NAME are correctly set.")
print("Also, verify that the table and fields exist and the token has appropriate permissions.")