Skip to main content

Upload Attachments

This article provides an example of how to use the Upload Attachments API to upload attachments to the AITable server, and how to add them to the attachment field.

For example, to add a local picture to the field Image, which field type is "attachment":

Add an image to a record

To accomplish this, first use the Upload attachments API to upload the local picture to the AITable server, then use the Create Records API / Update Records API to add the picture to the Image.

You can follow the steps below:

Step 1: Upload a local picture to the AITable server using API

  1. Get your API Token.(How to get it)

  2. Get the ID of the datasheet.(How to get it)

  3. Get the absolute path of your local picture.

  4. Open the terminal on your computer, execute the following code, and send the query request to the server (assuming datasheetId is dstWUHwzTHd2YQaXEE, the path of local pictures is /Users/coco/Documents/3.jpg):

    curl -X POST \
    https://aitable.ai/fusion/v1/datasheets/dstWUHwzTHd2YQaXEE/attachments \
    -H 'Authorization: Bearer {Your API Token}' \
    -F 'file=@/Users/coco/Documents/3.jpg'
  5. The server returns the following JSON data, below the "data" is all uploaded successful attachment information:

    For the meaning of each parameter in the response, please check the API Reference.

        {
    "code": 200,
    "success": true,
    "data": {
    "token": "space/2021/06/30/d336232203054effb819231a3426d40d",
    "mimeType": "image/jpeg",
    "size": 229426,
    "height": 1024,
    "width": 1792,
    "name": "3.jpg",
    "url": "https://s1.aitable.ai/space/2021/06/30/d336232203054effb819231a3426d40d"
    },
    "message": "SUCCESS"
    }

Step 2: Add the uploaded picture to the attachment field

After uploading the picture using the API, you can use the returned JSON data and the Create Records API / Update Records API to insert the picture into the attachment field.

The data structure is as follows, where Image​ is the name of the attachment field. Its value is an array (because an attachment cell can contain multiple attachments), and the elements of the array are objects from the data object in response to the HTTP request, which you can refer to the example return data in Step 5.

curl -X PATCH \
https://aitable.ai/fusion/v1/datasheets/dstWUHwzTHd2YQaXEE/records \
-H 'Authorization: Bearer {Your API Token}' \
-H 'Content-Type: application/json' \
-d '{
"records": [
{
"recordId": "recV3ElniQavTNyJG",
"fields": {
"Image": [
{
"token": "space/2021/06/30/d336232203054effb819231a3426d40d",
"mimeType": "image/jpeg",
"size": 229426,
"height": 1024,
"width": 1792,
"name": "3.jpg",
"url": "https://s1.aitable.ai/space/2021/06/30/d336232203054effb819231a3426d40d"
}
]
}
}
]
}'

Now the picture has been added to the datasheet successfully.

Image added

Q & A

1. I have attachments uploaded on other servers, can I use that URL to update the record?

No, you can only use the attachment URLs that were returned by the AITable to update the record.

2. I have uploaded attachments to other servers, how can I batch upload them to the attachment field on AITable

Here is a third-party browser plugin (APITable URL to Image Converter) that can automatically convert URL fields into attachments and fill them into the attachment field.