> ## Documentation Index
> Fetch the complete documentation index at: https://guides.curiousmints.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Fetch Data from Airtable Using pyairtable

> Learn how to retrieve data from Airtable using the pyairtable library

<Info>
  **Prerequisite** You should have an API key from Airtable and have it stored in a .env file.
</Info>

## Introduction

This guide demonstrates how to fetch data from an Airtable table using the `pyairtable` library in Python. `pyairtable` makes it super easy to interact with Airtable. You'll learn how to set up your environment, configure your API key, and query records from an Airtable table. You can find more details in the [pyairtable documentation](https://pyairtable.readthedocs.io).

<Accordion title="Show me the code" icon="code">
  ```python fetch_airtable_data.py theme={null}
  import os
  from dotenv import load_dotenv
  from pyairtable import Api
  from pyairtable.formulas import match

  # Load environment variables from .env file
  load_dotenv()

  # Get Airtable API key and base ID from environment variables
  airtable_api_key = os.getenv('AIRTABLE_API_KEY')
  airtable_base_id = os.getenv('AIRTABLE_BASE_ID')
  airtable_table_name = os.getenv('AIRTABLE_TABLE_NAME')

  # Initialize the Airtable API and table
  api = Api(airtable_api_key)
  table = api.table(airtable_base_id, airtable_table_name)

  # Define the formula to match records (optional)
  # Example formula to filter records where the "Name" field is "John Doe"
  formula = match({"Name": "John Doe"})  # Modify as needed or set to None if no filtering is required

  # Fetch all records, applying the formula if it exists
  records = table.all(formula=formula)

  # Print the retrieved records
  for record in records:
      print(record)
  ```
</Accordion>

## Setup

### Step 1: Install Required Packages

First, install the necessary packages using pip:

```sh theme={null}
pip install pyairtable python-dotenv
```

### Step 2: Create a .env File

Create a `.env` file in the project root with your Airtable API key and base ID:

```plaintext theme={null}
AIRTABLE_API_KEY=your_airtable_api_key
AIRTABLE_BASE_ID=your_airtable_base_id
AIRTABLE_TABLE_NAME=your_airtable_table_name
```

You can get your Airtable personal access token from [Airtable Personal Access Tokens](https://airtable.com/create/tokens). Personal access tokens are used to authenticate requests to Airtable's API. You can learn more about them at [Airtable Developer Documentation](https://airtable.com/developers/web/guides/personal-access-tokens).

## Fetching Data from Airtable

### Step 3: Create the Python Script

Create a Python script named `fetch_airtable_data.py` with the following content:

```python fetch_airtable_data.py theme={null}
import os
from dotenv import load_dotenv
from pyairtable import Api
from pyairtable.formulas import match

# Load environment variables from .env file
load_dotenv()

# Get Airtable API key and base ID from environment variables
airtable_api_key = os.getenv('AIRTABLE_API_KEY')
airtable_base_id = os.getenv('AIRTABLE_BASE_ID')
airtable_table_name = os.getenv('AIRTABLE_TABLE_NAME')

# Initialize the Airtable API and table
api = Api(airtable_api_key)
table = api.table(airtable_base_id, airtable_table_name)

# Define the formula to match records (optional)
# Example formula to filter records where the "Name" field is "John Doe"
formula = match({"Name": "John Doe"})  # Modify as needed or set to None if no filtering is required

# Fetch all records, applying the formula if it exists
records = table.all(formula=formula)

# Print the retrieved records
for record in records:
    print(record)
```

### Step 4: Run the Script

Ensure you have the `.env` file in the same directory as the script. Then, execute the script:

```sh theme={null}
python fetch_airtable_data.py
```

## Conclusion

You have successfully retrieved data from an Airtable table using the `pyairtable` library with the new `Api.table()` method! This guide provided a basic example to get you started. You can now expand on this by customizing the queries and handling different tables and fields in your Airtable base.
