Python Client Library for Airtable

0.4.8 · maintenance · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Airtable client and fetch all records from a specified table. It also includes an example of creating a new record. Authentication should use an Airtable Personal Access Token (PAT) loaded from an environment variable for security, as legacy API keys are deprecated.

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.")

view raw JSON →