Hey everyone, I’m pretty new to working with APIs and I’m stuck on something basic. I’m trying to add a new record to my Airtable database using Python but keep getting authentication errors.
See how I separated the headers from the actual data? Also use json=data instead of passing data as the second parameter. This makes requests properly serialize your dictionary as JSON.
Double check your token is valid and has write permissions for that base too.
I made this exact mistake about a year ago when learning APIs. You’re mixing up authorization info with your data payload - auth needs to go in HTTP headers, not the request body. Look at your curl command - see how the -H flags are separate from the -d data? The requests library works the same way. You’ve got everything in one dictionary, but the server wants auth details as headers, not in the JSON body. Split your payload into two dictionaries and pass them separately to requests.post(). Also use the json parameter instead of passing data as the second argument - it handles JSON serialization properly.
you’ve got your headers and data mixed up. the authorization token goes in the headers, not in your json payload. here’s the fix: headers = {"Authorization": "Bearer YOUR_TOKEN", "Content-Type": "application/json"} then do requests.post(BASE_URL, json=payload, headers=headers) where payload just has your fields dict.
I hit this exact issue switching from curl to Python requests. You’re mixing up headers with data - they need to be separate parameters in requests.post(). Think of it this way: Authorization headers go with the HTTP request itself, not in the JSON body that gets sent to the server. Right now you’re dumping everything into one payload dictionary, but requests wants headers as their own parameter. Try this: import requests headers = { “Authorization”: “Bearer YOUR_TOKEN”, “Content-Type”: “application/json” } data = { “fields”: { “Product”: “testproduct”, “Amount”: “3”, “User_ID”: [“testrecord”] } } response = requests.post(BASE_URL, json=data, headers=headers) The fix is using headers=headers as a separate parameter. This sends those values as HTTP headers instead of stuffing them in the request body.