Upload a file to Box and list all files in the root folder
The Upload file endpoint allows you to upload a single file to Box through the upload domain https://upload.box.com/api/2.0/files/content.
The maximum file size is 50MB. For larger files, use the .
A pre-built application using Client Credentials Grant (CCG) authentication is created for you to get started. This app is authorized automatically, so you don’t need separate admin approval before making API calls.Create a developer token to authenticate your requests. Go to the Developer Console > your pre-built application and find the App Details section. Locate the Developer Token field, select Generate Developer Token, and copy it.
Developer tokens expire one hour after you generate them.
We prepared an example invoice document for you to upload. Create a root folder for your project, download the file , and save it in the folder.
Python
Node.js
Swift
Java
.NET
Create and activate a Python virtual environment.
python3 -m venv .venvsource .venv/bin/activate
Install the Box Python SDK by running the following command using .
pip install "boxsdk>=10"
Make sure you use the most up-to-date version number of the Python SDK. Refer to the
.
For production codebase, always store your credentials in a .env file and add .env to your .gitignore.
Then call the upload API as follows.
with open("demo-invoice-20tax-2.pdf", "rb") as file_content_stream: uploaded_file = client.uploads.upload_file( UploadFileAttributes( name="demo-invoice-20tax-2.pdf", parent=UploadFileAttributesParentField(id="0"), ), file_content_stream, )items = client.folders.get_folder_items("0")print(json.dumps(items.to_dict(), indent=2))
The get_folder_items command lists all files in the root folder. Execute the script to upload the file and list all files.
Install the Box Node.js SDK by running the following command using . The box package installs the Box Node SDK and the Box CLI as direct dependencies. Node.js 22 or later is required.
npm install box
Existing projects can keep using npm install box-node-sdk — that package is still supported.
A pre-built application is created for you to get started. Create a developer token to authenticate your requests. Go to the Developer Console > your pre-built application and find the App Details section. Locate the Developer Token field, select Generate Developer Token, and copy it.Obtain a client with your developer token:
const { BoxClient, BoxDeveloperTokenAuth } = require('box/sdk');const auth = new BoxDeveloperTokenAuth({ token: 'YOUR_DEVELOPER_TOKEN' });const client = new BoxClient({ auth });
Developer tokens expire one hour after you generate them.
The getFolderItems command lists all files in the root folder. Execute the script to upload the file and list all files.
Install the Box Swift SDK by running the following command using .
swift package update
A pre-built application is created for you to get started. Create a developer token to authenticate your requests. Go to the Developer Console > your pre-built application and find the App Details section. Locate the Developer Token field, select Generate Developer Token, and copy it.
Developer tokens expire one hour after you generate them.
Create a request body with the required parameters: attributes (name and parent id) and a file stream.
guard let fileStream = InputStream(url: URL(fileURLWithPath: "demo-invoice-20tax-2.pdf")) else { fatalError("Could not read a file")}let requestBody = UploadFileRequestBody( attributes: UploadFileRequestBodyAttributesField( name: "demo-invoice-20tax-2.pdf", parent: UploadFileRequestBodyAttributesParentField(id: "0") ), file: fileStream)let files = try await client.uploads.uploadFile(requestBody: requestBody)if let file = files.entries?[0] { print("File uploaded with id \(file.id), name \(file.name!)")}let items = try await client.folders.getFolderItems(folderId: "0")if let entries = items.entries { for entry in entries { switch entry { case let .fileMini(file): print("file \(file.name!) [\(file.id)]") case let .folderMini(folder): print("folder \(folder.name!) [\(folder.id)]") case let .webLinkMini(webLink): print("webLink \(webLink.name!) [\(webLink.id)]") } }}
The getFolderItems command lists all files in the root folder. Execute the script to upload the file and list all files.
A pre-built application is created for you to get started. Create a developer token to authenticate your requests. Go to the Developer Console > your pre-built application and find the App Details section. Locate the Developer Token field, select Generate Developer Token, and copy it.
Developer tokens expire one hour after you generate them.
GradleInstall the Box Java SDK by adding the following dependency to the build.gradle file.
implementation 'com.box:box-java-sdk:10.9.0'
Make sure you use the most up-to-date version number of the Java SDK. Refer to the
.
BoxDeveloperTokenAuth auth = new BoxDeveloperTokenAuth("YOUR_DEVELOPER_TOKEN");BoxClient client = new BoxClient(auth);
Pass new UploadFileRequestBody to client.getUploads().uploadFile: attributes (UploadFileRequestBodyAttributesField with name and UploadFileRequestBodyAttributesParentField for folder id) and an InputStream for the file bytes.
FileInputStream fileContentStream = new FileInputStream("demo-invoice-20tax-2.pdf");client.getUploads().uploadFile( new UploadFileRequestBody( new UploadFileRequestBodyAttributesField( "demo-invoice-20tax-2.pdf", new UploadFileRequestBodyAttributesParentField("0") ), fileContentStream ));client.getFolders().getFolderItems("0").getEntries().forEach(item -> { System.out.println(item);});
The getFolderItems command lists all files in the root folder. Execute the script to upload the file and list all files.
.NET FrameworkTo install the .NET SDK in the .NET framework, run the following command using
the package manager.
PM> Install-Package Box.V2.Core
A pre-built application is created for you to get started. Create a developer token to authenticate your requests. Go to the Developer Console > your pre-built application and find the App Details section. Locate the Developer Token field, select Generate Developer Token, and copy it.
Developer tokens expire one hour after you generate them.
.NET CoreTo install the .NET SDK in the .NET Core framework, run the following command using
the package manager.
PM> Install-Package Box.V2.Core
Obtain a client with your developer token:
using Box.Sdk.Gen;var auth = new BoxDeveloperTokenAuth(token: "YOUR_DEVELOPER_TOKEN");var client = new BoxClient(auth: auth);
Await client.Uploads.UploadFileAsync with new UploadFileRequestBody: UploadFileRequestBodyAttributesField (name and parent id) and a Stream (for example from File.OpenRead).
using var fileContentStream = File.OpenRead("demo-invoice-20tax-2.pdf");var files = await client.Uploads.UploadFileAsync( requestBody: new UploadFileRequestBody( attributes: new UploadFileRequestBodyAttributesField( name: "demo-invoice-20tax-2.pdf", parent: new UploadFileRequestBodyAttributesParentField(id: "0")), file: fileContentStream));var file = files.Entries[0];Console.WriteLine($"File uploaded with id {file.Id}, name {file.Name}");var items = await client.Folders.GetFolderItemsAsync(folderId: "0");if (items.Entries != null){ foreach (var item in items.Entries) { if (item.FileFull != null) { Console.WriteLine(item.FileFull.Name); } else if (item.FolderMini != null) { Console.WriteLine(item.FolderMini.Name); } else if (item.WebLink != null) { Console.WriteLine(item.WebLink.Name); } }}
The getFolderItems command lists all files in the root folder. Execute the script to upload the file and list all files.
Congratulations! You’ve uploaded your first file to Box and listed all files in the root folder.