Trouble inserting data into Airtable via API

I’m stuck trying to add a new row to my Airtable using their API. Here’s what I’ve got so far:

import requests

api_url = 'https://api.airtable.com/v0/my_base_id/my_table_name'
api_key = 'my_secret_key'

headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json'
}

new_row = {
    'fields': {
        'Column1': 'New Value'
    }
}

response = requests.post(api_url, json=new_row, headers=headers)
print(response.text)

When I run this, I get an error about invalid parameters. I’ve double-checked my API key and base ID, but it’s still not working. Am I missing something obvious? I’ve been banging my head against this for hours and could really use some help. Thanks!

Hey John, I’ve run into this before. One thing to check is if you’re using the correct HTTP method. For creating new records, you need to use POST, which you’re doing correctly. However, make sure your ‘fields’ structure in new_row matches your Airtable exactly. Another tip: try printing out the full response, not just the text. Something like print(response.status_code, response.json()) can give you more detailed error info. Also, double-check that your API key has write permissions for that specific base and table. Sometimes it’s a permissions issue that’s not immediately obvious. If you’re still stuck, Airtable’s API docs have some good troubleshooting tips.

Hey John, I’ve been there and it can be frustrating. One thing that caught me out was the case sensitivity of field names. Make sure ‘Column1’ in your code matches exactly how it’s written in Airtable. Also, try adding more error handling to your script. Something like:

try:
    response = requests.post(api_url, json=new_row, headers=headers)
    response.raise_for_status()
    print('Success:', response.json())
except requests.exceptions.RequestException as e:
    print('Error:', e)
    print('Response:', response.text)

This will give you more detailed error info. If you’re still stuck, try creating a minimal example with just one field and build up from there. Sometimes simplifying helps pinpoint the issue. Good luck!

hey john, i had similar issues. make sure ur fields in the new_row match EXACTLY with ur airtable column names (caps n all). also, try adding ‘typecast’: True to ur new_row dict. that helped me when i had weird data type probs. good luck!