Help

Python API code not working

Topic Labels: Automations
660 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Gabriel_Ferguso
4 - Data Explorer
4 - Data Explorer

I have to upload a file to Airtable. I don’t quite understand the documentation for file upload. I have this code that is in python that should upload a file to airtable via a link but it gives me a 422 error. How can I fix this?


ATBASEID = "****************************"
ATTABLENAME = "DEV"

AIRTABLE_API_KEY = "*************************"
ENDPOINT = "https://api.airtable.com/v0/" + \
    ATBASEID + "/" + ATTABLENAME
HEADERS2 = {"Authorization": f"Bearer {AIRTABLE_API_KEY}", "Content-Type": "application/json"}

data2 = [
    {
        "id": "attqyuWADmKW9DgDG",
        "size": 26317,
        "url": "https://www.filepicker.io/api/file/5YTJXioCQG0tYWPw6OPw",
        "type": "image/jpeg",
        "filename": "33823_3_xl.jpg",
        "thumbnails": {
            "small": {
                 "url": "https://www.filepicker.io/api/file/Dy5gioxaShSUvHX0LgIC",
                 "width": 54,
                 "height": 36
             },
            "large": {
                 "url": "https://www.filepicker.io/api/file/ueYi00yRiqhuUn420UZA",
                 "width": 197,
                 "height": 131
             }
        }
    }
]
r_1 = requests.patch(ENDPOINT, json=data2, headers=HEADERS2)
print(r_1.status_code)

All help is greatly appreciated.

1 Reply 1

Hi @Gabriel_Ferguson - I’ve got a similar script that uploads an attachment to Airtable based on a URL. The key part of the script is:

data = {
  "fields": {
    "Print Preview": [
      {
        "url": image_url
      }
    ]
  }
}
# print(data)
response = requests.patch(api_url, headers=headers, json=data)

So you only need to pass the URL to the API, not the size, type, filename and so on. All of these attributes will be returned if you GET data from the API, but they aren’t needed to POST data.

Note that in my script Print Preview is an attachment field and you pass an array of objects, each object containing the single attribute “url”