Python requests authentication issue when adding records to Airtable database

[beginner help needed]

I’m working on adding new entries to my Airtable database using Python requests library. The official documentation shows this curl example:

curl -X POST https://api.airtable.com/v0/your_base_id/table_name \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "fields": {
      "Product": "Laptop",
      "Count": "3",
      "User_ID": [
        "rec123456"
      ]
    }
  }'

Here’s my Python attempt:

import requests

BASE_URL = "https://api.airtable.com/v0/your_base_id/table_name"

payload = {
    "Authorization": "Bearer YOUR_TOKEN",
    "Content-Type": "application/json",
    "fields": {
        "Product": "smartphone",
        "Count": "2",
        "User_ID": ["rec789012"]
    }
}

response = requests.post(BASE_URL, payload)
print(response.json())

But I keep getting this authentication error:

{'error': {'type': 'AUTHENTICATION_REQUIRED', 'message': 'Authentication required'}}

What’s the correct way to handle the authentication headers in Python requests?

yeah, keep headers separate from your data payload. and don’t forget to use your actual token instead of the ‘YOUR_TOKEN’ placeholder - made that mistake before lol. add some error handling too like response.raise_for_status() to catch issues easier.

You’re putting the Authorization header in the wrong place. It’s mixed up with your payload data.

Here’s the fix:

import requests

BASE_URL = "https://api.airtable.com/v0/your_base_id/table_name"

headers = {
    "Authorization": "Bearer YOUR_TOKEN",
    "Content-Type": "application/json"
}

payload = {
    "fields": {
        "Product": "smartphone",
        "Count": "2",
        "User_ID": ["rec789012"]
    }
}

response = requests.post(BASE_URL, headers=headers, json=payload)
print(response.json())

Two changes:

  1. Move Authorization and Content-Type into a separate headers dictionary
  2. Use json=payload instead of just payload

I’ve made this same mistake converting curl commands to Python. Those -H flags always go in headers, not the request body.