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":
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
Get your API Token.(How to get it)
Get the ID of the datasheet.(How to get it)
Get the absolute path of your local picture.
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
- Javascript SDK
- Python SDK
curl -X POST \
https://aitable.ai/fusion/v1/datasheets/dstWUHwzTHd2YQaXEE/attachments \
-H 'Authorization: Bearer {Your API Token}' \
-F 'file=@/Users/coco/Documents/3.jpg'Note: Need to Download and initialize Javascript SDK first, and then execute the following command.
import { APITable } from 'apitable';
const apitable = new APITable({
token: 'Your API Token',
});
const datasheet = apitable.datasheet("dstWUHwzTHd2YQaXEE");
// in the node environment
const file = fs.createReadStream('/Users/coco/Documents/3.jpg')
try {
const resp = await datasheet.upload(file)
if (resp.success) {
const uploaded_attachments = resp.data
}
} catch (error) {
console.error(error)
}Note: You need to download and initialize the Python SDK first, and then execute the following command.
from apitable import Apitable
apitable = Apitable("Your API Token")
dst = apitable.datasheet("dstWUHwzTHd2YQaXEE")
# Upload a file to the specified datasheet
file = dst.upload_file("/Users/coco/Documents/3.jpg")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
- Javascript SDK
- Python SDK
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"
}
]
}
}
]
}'
Note: Need to Download and initialize Javascript SDK first, and then execute the following command.
import { APITable } from 'apitable';
const apitable = new APITable({
token: 'Your API Token',
});
const datasheet = apitable.datasheet("dstWUHwzTHd2YQaXEE");
// in the node environment
const file = fs.createReadStream('/Users/coco/Documents/3.jpg')
try {
const resp = await datasheet.upload(file)
if (resp.success) {
const uploaded_attachments = resp.data
await apitable.datasheet('dstWUHwzTHd2YQaXEE').records.create([{
'Image': [uploaded_attachments]
}])
}
} catch (error) {
console.error(error)
}
Note: You need to download and initialize the Python SDK first, and then execute the following command.
from apitable import Apitable
apitable = Apitable("Your API Token")
dst = apitable.datasheet("dstWUHwzTHd2YQaXEE")
# Upload a file to the specified datasheet
file = dst.upload_file("/Users/coco/Documents/3.jpg")
# Update the "Image" field of a specified record
record = dst.records.get(ProductID="100012")
record.Image = [file]
Now the picture has been added to the datasheet successfully.
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.