Box Developer Documentation

Join BoxWorks 2024 to discover what's possible with content and AI!

Register now!

Upload File Version

Upload File Version

To upload a new version of a file to Box via direct upload, make an API call to the POST /files/:id/content API with the content of the file, the desired file name, and the folder ID.

cURL
curl -i -X POST "https://upload.box.com/api/2.0/files/12345/content" \
     -H "authorization: Bearer <ACCESS_TOKEN>" \
     -H "content-type: multipart/form-data" \
     -F attributes='{"name":"Contract.pdf", "parent":{"id":"11446498"}}' \
     -F file=@<FILE_NAME>
TypeScript Gen
await client.uploads.uploadFileVersion(file.id, {
  attributes: {
    name: file.name!,
  } satisfies UploadFileVersionRequestBodyAttributesField,
  file: generateByteStream(20),
} satisfies UploadFileVersionRequestBody);
Python Gen
client.uploads.upload_file_version(
    file.id, UploadFileVersionAttributes(name=file.name), generate_byte_stream(20)
)
.NET Gen
await client.Uploads.UploadFileVersionAsync(fileId: file.Id, requestBody: new UploadFileVersionRequestBody(attributes: new UploadFileVersionRequestBodyAttributesField(name: NullableUtils.Unwrap(file.Name)), file: Utils.GenerateByteStream(size: 20)));
Java
BoxFile file = new BoxFile(api, "id");
FileInputStream stream = new FileInputStream("My File.txt");
file.uploadNewVersion(stream);
Python
file_id = '11111'
file_path = '/path/to/file.pdf'

updated_file = client.file(file_id).update_contents(file_path)
print(f'File "{updated_file.name}" has been updated')
.NET
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
    BoxFile file = await client.FilesManager
        .UploadNewVersionAsync("File v2.pdf", "11111", fileStream);
}
Node
var fs = require('fs');
var stream = fs.createReadStream('/path/to/file.pdf');
client.files.uploadNewFileVersion('11111', stream)
	.then(file => {
        /* file -> {
            total_count: 1,
            entries: 
            [ { type: 'file',
                id: '11111',
                file_version: 
                    { type: 'file_version',
                    id: '22222',
                    sha1: '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' },
                sequence_id: '0',
                etag: '0',
                sha1: '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33',
                name: 'My File.pdf',
                description: '',
                size: 68431,
                path_collection: 
                    { total_count: 1,
                    entries: 
                    [ { type: 'folder',
                        id: '0',
                        sequence_id: null,
                        etag: null,
                        name: 'All Files' } ] },
                created_at: '2017-05-16T15:18:02-07:00',
                modified_at: '2017-05-16T15:18:02-07:00',
                trashed_at: null,
                purged_at: null,
                content_created_at: '2017-05-16T15:18:02-07:00',
                content_modified_at: '2017-05-16T15:18:02-07:00',
                created_by: 
                    { type: 'user',
                    id: '33333',
                    name: 'Test User',
                    login: 'test@example.com' },
                modified_by: 
                    { type: 'user',
                    id: '33333',
                    name: 'Test User',
                    login: 'test@example.com' },
                owned_by: 
                    { type: 'user',
                    id: '33333',
                    name: 'Test User',
                    login: 'test@example.com' },
                shared_link: null,
                parent: 
                    { type: 'folder',
                    id: '0',
                    sequence_id: null,
                    etag: null,
                    name: 'All Files' }
                item_status: 'active' } ] }
        */
    })
iOS
let data = "updated file content".data(using: .utf8)
let task: BoxUploadTask = client.files.uploadVersion(
    forFile: "11111",
    name: "New file name.txt",
    contentModifiedAt: "2019-08-07T09:19:13-07:00",
    data: data
) { (result: Result<File, BoxSDKError>) in
    guard case let .success(file) = result else {
        print("Error uploading file version")
        return
    }

    print("New version of \(file.name) was uploaded")
}

// To cancel upload
if someConditionIsSatisfied {
    task.cancel()
}

Preflight check

To prevent wasting time and bandwidth uploading a file that is going to be rejected it is recommended to perform a pre-flight check before uploading the file.

Request Format

The request body of this API uses a content type of multipart/form-data. This is used to transmit two parts, namely the file attributes and the file's actual content.

The first part is called attributes and contains a JSON object with information about the file, including the name of the file and the id of the parent folder.

The following is an example a test.txt being uploaded to the root folder of the user.

POST /api/2.0/files/123/content HTTP/1.1
Host: upload.box.com
Authorization: Bearer [ACCESS_TOKEN]
content-length: 343
content-type: multipart/form-data; boundary=------------------------9fd09388d840fef1

--------------------------9fd09388d840fef1
content-disposition: form-data; name="attributes"

{"name":"test.txt", "parent":{"id":"0"}}
--------------------------9fd09388d840fef1
content-disposition: form-data; name="file"; filename="test.txt"
content-type: text/plain

Test file text.

--------------------------9fd09388d840fef1--

The attributes JSON part of the multi-part body must come before the file part of the multipart form data. When out of order, the API will return a HTTP 400 status code with an error code of metadata_after_file_contents.

Options

To learn more about all the parameters available when uploading files, head over to the reference documentation for this API call. These parameters include a content-md5 that can be set to ensure a file is not corrupted in transit, and the ability to explicitly specify the file creation time at a different time than the upload time.

For file versions an additional if-match header can be passed along to prevent overwriting a file that has already been updated since the application last inspected the content.

Restrictions

Direct uploads are limited to a maximum file size of 50MB. For larger files, please use the chunked upload APIs.

Upload limits are dictated by the type of account of the authenticated user. More information can be found in our community article on this topic.