Need help (update row API call) to attach a file to a specific record after successfully uploading it to the server

Hi,
We’ve created several scripts that take data from one row in SeaTable, send it to our API server and return a PDF document that we want to add to a specific record from the initial row.
The script is working perfectly but we are stuck for the last bit (putting the PDF file in the record (File Type Column).

We are using the REST API; We can get data, and modify tables/rows of type text, etc. But we are stuck at trying to “attach” documents.

Our goal is to update/attach a PDF file to an existing row, from the table “CCC”

Following documentation, it’s easy getting the “upload link”:

{
"upload_link": "https://cloud.seatable.io/seafhttp/upload-api/deeacb2d-fcac-471f-8640-662ca52acdf7",
"parent_path": "/asset/529d86b6-0c31-422a-bf72-7ebc2c669928",
"img_relative_path": "images/2022-08",
"file_relative_path": "files/2022-08"
}

Then with the information received, uploading the document is not an issue, api returns :

[{
"name": "test.pdf",
"id": "0d1e3593a845af0975c4cc5b1ec180cab0b7393c",
"size": 14734
}]

At this point we have the PDF updated, and ready to attach it to a “Documents” column into the table Company

Information needed to construct the next call

{
'name' => ‘test.pdf’,
'size' => $file_size,
'type' => 14734,
'url' => [https://cloud.seatable.io/workspace/3/asset/529d86b6-0c31-422a-bf72-7ebc2c669928/files/2022-08/test.pd](https://cloud.seatable.io/workspace/3/asset/529d86b6-0c31-422a-bf72-7ebc2c669928/files/2022-08/test.pdf)
}

We cannot find a full example on docs/forums for how to attach this new uploaded filed to the table “CCC"

Updating rows refers to “append a row” for files updates/attachments, but example there is specific for “adding rows”

We are looking for a full example of how to update/attach a file to an existing row. Does anyone can help?

Thanks a lot

To update an existing row using Python script:

file_info = {
  'name': 'test.pdf',
  'size': file_size,
  'type': 'file',
  'url': 'https://xxx.com/workspace/3/asset/529d86b6-0c31-422a-bf72-7ebc2c669928/files/2022-08/test.pd'
}
file_column_info = [file_info]

row_data = { 'file-column-name': file_column_info }

base.update_row('Table1', 'the-row-id', row_data)

Note:

  • row_data is a dict and should only contains the columns you want to update.
  • type field for a file should always be string ‘file’.

Hello Daniel,

Many thanks for your help and the python script. I think we saw that, and tried to make a REST API version of it, but we are still trying the find what this would be in REST API.
If you happen to know please let us know…if we find it, we will post it here

Hey there,
I tried to write a complete script to upload a file to a SeaTable base. I hope this explains the process in more details…

#!/bin/bash

# EXPLANATION:
# this script creates a temporary text file in the same directory of this script.
# afterwards this file is uploaded to a seatable base
# the script explains how to upload the file as a new row or to add the file to an existing row
# ATTENTION: if you update a row that has already a file in it, the file will be replaced by the new one
# HINT: remove the '--silent' to get the output of the curl requests.

# Required input variables:
SEATABLE_SERVER='https://cloud.seatable.io'
BASE_TOKEN='c5017449712d5261b5c4ffc9b6a7f7b31b39a337'
TABLE_NAME='Table1'
# my table structure is:
# Name          [TEXT]
# Description   [LONGTEXT]
# File          [FILE]

# Create a random file that we can upload (filename is like ab3sr.txt)
RANDOM_STRING=`echo $RANDOM | md5sum | head -c 5`
RANDOM_FILE="${RANDOM_STRING}.txt"
#echo $RANDOM_FILE
echo "hello world" > ./${RANDOM_FILE}

# let's do this ...
#######################################################################

##
## Step 1: get an upload link
##

output=`curl --silent --location --request GET "${SEATABLE_SERVER}/api/v2.1/dtable/app-upload-link/" \
--header "Authorization: Token ${BASE_TOKEN}" \
--header 'Accept: application/json; indent=4'`

# this returns something like this:
#{
#    "upload_link": "https://cloud.seatable.io/seafhttp/upload-api/80885db6-9361-4197-a433-16765ee017af",
#    "parent_path": "/asset/d38272f8-9c69-40cf-8b5e-aff8134bf4f4",
#    "img_relative_path": "images/2022-08",
#    "file_relative_path": "files/2022-08"
#}

# store the return values to variables
UPLOAD_URL=`echo ${output} | jq -r '.upload_link'`
PAR_PATH=`echo ${output} | jq -r '.parent_path'`
REL_PATH=`echo ${output} | jq -r '.file_relative_path'`
BASE_UUID=`echo ${PAR_PATH} | cut -c 8-`
#echo ${UPLOAD_URL}

##
## Step 2 : upload the document
##

output2=`curl --silent --location --request POST "${UPLOAD_URL}?ret-json=true" \
--header "Authorization: Token ${BASE_TOKEN}" \
--form 'file=@"./'${RANDOM_FILE}'"' \
--form "parent_dir=${PAR_PATH}" \
--form 'replace="0"' \
--form "relative_path=${REL_PATH}"`

# returns something like this:
#[{"name": "test.txt", "id": "0000000000000000000000000000000000000000", "size": 0}]

NAME=`echo ${output2} | jq -r '.[].name'`
SIZE=`echo ${output2} | jq -r '.[].size'`
echo "NAME: ${NAME}"
echo "SIZE: ${SIZE}"

##
## Step 3: get base access token and build the "url" for the next api call
##

output3=`curl --silent --location --request GET "${SEATABLE_SERVER}/api/v2.1/dtable/app-access-token/" \
--header 'Accept: application/json; charset=utf-8; indent=4' \
--header "Authorization: Token ${BASE_TOKEN}"`

BASE_UUID=`echo ${output3} | jq -r '.dtable_uuid'`
ACCESS_TOKEN=`echo ${output3} | jq -r '.access_token'`
WORKSPACE=`echo ${output3} | jq -r '.workspace_id'`
URL="${SEATABLE_SERVER}/workspace/${WORKSPACE}${PAR_PATH}/${REL_PATH}/${NAME}"
echo "URL: ${URL}"
echo "BASE_UUID: ${BASE_UUID}"

##
## Step 4a: attach the file to the base as a new row
##

curl --silent --location --request POST "${SEATABLE_SERVER}/dtable-server/api/v1/dtables/${BASE_UUID}/rows/" \
--header "Authorization: Token ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw '{
    "table_name": "'${TABLE_NAME}'",
    "row": {
        "Name": "New row",
        "Description": "abc",
        "File": [
            {
                "name": "'${NAME}'",
                "size": '${SIZE}',
                "type": "file",
                "url": "'${URL}'"
            }
        ]
    }
}'

# returns something like
#{"0000":"New row","W39K":"abc","dHrW":[{"name":"1e23b.txt","size":0,"type":"file","url":"https://cloud.seatable.io/workspace/2/asset/d38272f8-9c69-40cf-8b5e-aff8134bf4f4/files/2022-08/1e23b.txt"}],"_id":"Yw7snjIERvudEIpqElnT2Q"}


##
## Step 4b: attach the file to the base to an existing row
## ATTENTION: if a file already exists, it will be overwritten...
##

ROW_ID="VqnFdqTwTc2zn5YARRt7wA"

curl --location --request PUT "${SEATABLE_SERVER}/dtable-server/api/v1/dtables/${BASE_UUID}/rows/" \
--header "Authorization: Token ${ACCESS_TOKEN}" \
--header 'Accept: application/json' \
--header 'Content-type: application/json' \
--data-raw '{
    "table_name": "'${TABLE_NAME}'",
    "row_id": "'${ROW_ID}'",
    "row": {
        "File": [
            {
                "name": "'${NAME}'",
                "size": '${SIZE}',
                "type": "file",
                "url": "'${URL}'"
            }
        ]
    }
}'

# clean up (if you want)
#rm ./${RANDOM_FILE}

Best regards,
Christoph

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.