Search code examples
python-3.xapipython-requestsairtable

Accessing Airtable's Pagination Using Python's Requests Library


How can I access Airtable's pagination by including a given offset in either the url or query?

My attempt below at including in the url has failed with the error "KeyError: 'offset'".

I've been able to access the 1st offset per the function at_page(). For simplicity's sake, I took the result from the function and hardcoded it to the variable offset_01.

Thank you!

import requests
import pandas as pd

offset_01 = 'itrCG3VDp2c34x1j1/recKTJGYMICe13iA8'
url = 'https://api.airtable.com/v0/PRIVATETABLEKEY/accounts' + \
    '&offset=' + offset_01
headers = {'Authorization': 'Bearer PRIVATEAPIKEY'}


# Calling API
response = requests.get(
    url,
    headers=headers,
)
json_response = response.json()


# Finding AT page offset

def at_page(at_json):
    df_initial = pd.DataFrame(at_json)
    at_offset = df_initial['offset'][0]

    return at_offset

offset = at_page(json_response)

Solution

  • Figured it out:

    It’s the params={‘offset’: ‘itrXXXXXX/recXXXXXX’} query that allows access to the next 100 records:

    # Calling API
    response = requests.get(
        url,
        headers=headers,
        params={'offset': 'itrXXXXXX/recXXXXXX'}
    )
    json_response = response.json()
    

    Hope this helps someone. Cheers!