Working with Airtable and Python

I’ve been trying to work with Airtable and Python recently. I have a table built and I have done this in the past with Shortcuts successfully. (thanks to Christopher Lawley’s YouTube video). I’m trying to replicate this behavior in Python but am having no luck. I’ve done a bunch of googling and found Python wrappers but they’re not working out either. Anybody have any luck with this?

1 Like

Maybe you could pick a wrapper, provide your sample code, which version of Python you are using, on which platform, and provide details of the error you are seeing? This wrapper looks to be in active development, so I see no obvious reason why it shouldn’t be expected to work.

I was able to do this :nerd_face:

Below is my code, which was created to take tasks from my Todoist and add them to Airtable.

import todoist, datetime
from airtable import airtable


# Airtable authentication
base_key = 'YOUR_BASE_KEY'
table_name = 'Daily Task Density'
api_key = 'TODOIST_API_KEY'

#Tells Airtable which table/base to use
table = airtable.Airtable(base_key, table_name, api_key=api_key)


#Todoist authentication
api = todoist.TodoistAPI(token='TODOIST_TOKEN')
api.sync()

# Gets today's date
currentDT = datetime.datetime.now()
today = currentDT.strftime("%Y" + "-" + "%m" + "-" + "%d")


# Iterates over each Todoist task due today and adds to Airtable (task name and priority)
task_name = ''
for item in api.state['items']:
	if item['due'] != None:
		if item['due']['date'] == today:
			task_name = item['content']
			priority = str(item['priority'])
			print(item['content'], item['due']['date'])
			table.insert({'Task': task_name, 'Priority': 'P' + priority})

I can certainly say that the API for Airtable could be wrapped a bit better for different languages as it took me some time to wrap my head around how to work with it initially.

Hopefully, the above code snippet helps. Please feel free to ask more questions though!

Airtable’s API is really transparent. You can get by quite easily with just the Requests and JSON modules.

Hey, Dr. Drang!

I tried out your example script and I believe I’m hitting their API, but I seem to getting a nondescript 422 error. Anything in my code you think could be causing it?

# imports
import requests
import json

movieTitle = 'Batman'
movieYear = '1989'
movieDirector = 'Tim Burton'
movieGenre = 'Action, Adventure'
movieRTRating = '72'
movieMyRating = '60'

# Add record to Airtable database.
airURL = 'https://api.airtable.com/v0/appPs8NuOpz0Tjb8t/Movie%20Ratings'
airHeaders = {
    'Authorization': 'Bearer anonymized api_key',
    'Content-Type': 'application/json'}
payload = {'fields': {
    'Title': movieTitle,
    'Year': movieYear,
    'Director': movieDirector,
    'Genre': movieGenre,
    'Rotten Tomatoes Score': movieRTRating,
    'My Score': movieMyRating}}
r = requests.post(airURL, headers=airHeaders, data=json.dumps(payload))

print(r)

Edit: I’ve gotten this to work. I ended up having to change the field types to all “Single Lines of Text” instead of using spceial field types. I see you can play around with typecast so I may play around with that.