Skip to content
Verified

Upload a file to Drive

The one call in this section that is not a JSON body, a multipart upload to the account's own Drive.

Every other call in this section sends api_key and its fields as one JSON body. upload_drive_file_user is the exception: the file itself has to travel as a multipart part, so api_key and the rest of the fields ride alongside it as form fields instead of JSON. The endpoint is still /cpi/upload_drive_file_user, same as any other method, multer is mounted ahead of the router for every route and only acts when the request is actually multipart.

Method

POST/cpi/upload_drive_file_user
user

Upload Drive File User

Uploads drive file to user drive.

This method has no declared field list

upload_drive_file_user is one of the handful of methods with an empty fields array in the registry (it takes a file, not typed parameters), so the interactive reference cannot show you its inputs. They are read directly from the request by the handler, and reproduced from source below.

Walkthrough

Send the file as multipart, not JSON

The file part must be named file, that is the only field name multer is configured to accept. Everything else is an ordinary form field on the same request: api_key, an optional path (the folder to place it in, defaults to /), an optional make_public boolean, and an optional tags value (a comma-separated string or a JSON array).

curl -s https://xuda.ai/cpi/upload_drive_file_user \
  -F "api_key=YOUR_API_KEY" \
  -F "path=/Reports" \
  -F "tags=quarterly,finance" \
  -F "file=@/local/path/q3-report.pdf"
const form = new FormData();
form.append('api_key', 'YOUR_API_KEY');
form.append('path', '/Reports');
form.append('tags', 'quarterly,finance');
form.append('file', fileInput.files[0]); // a File/Blob, e.g. from an <input type="file">

const res = await fetch('https://xuda.ai/cpi/upload_drive_file_user', {
  method: 'POST',
  body: form, // no content-type header: the browser sets the multipart boundary itself
});
const { code, data } = await res.json();
if (code > 0) console.log(data.file_url);
import requests

with open('/local/path/q3-report.pdf', 'rb') as fh:
    res = requests.post(
        'https://xuda.ai/cpi/upload_drive_file_user',
        data={'api_key': 'YOUR_API_KEY', 'path': '/Reports', 'tags': 'quarterly,finance'},
        files={'file': ('q3-report.pdf', fh, 'application/pdf')},
    )
body = res.json()
if body['code'] > 0:
    print(body['data']['file_url'])

The response

json
{
  "code": 760,
  "data": {
    "server_file_name": "drv_app_5d9e21_a91f0eaa.pdf",
    "filename": "q3-report.pdf",
    "file_url": "https://xuda.ai/user-drive/app_5d9e21/drv_app_5d9e21_a91f0eaa.pdf",
    "file_path": "/Reports",
    "bucket": { "xuda.ai": { "Bucket": "...", "Key": "...", "Location": "..." } }
  },
  "rename": false
}

Success answers code: 760, not the usual 1, branch on code > 0, not on a specific value. A name collision at the same path is not an error either: the file is stored under a disambiguated server_file_name and rename comes back true so you know the original name was not what actually got used. filename always echoes the original upload name regardless.

Errors

Because this method has no declared fields, none of these come back as the usual 'x' is a mandatory field message, they are the handler's own text in data, all at code: -400:

A missing or unauthorized api_key still fails the normal way, at -401, before the handler is ever reached. See Errors.

Next