Create a Folder and Upload a File

Learn how to create a new folder and upload a file using the Dropbox API.

In this lesson, we’ll start another namespace called files, and we’ll explore some of its important endpoints. Most of this namespace’s endpoints require us to enable the files.content.write and files.content.read permissions.

In this lesson, we’ll learn about the following two endpoint operations:

  • create_folder_v2
  • upload

Create a new folder

The /files/create_folder_v2 endpoint creates a new folder at the path specified. It uses an HTTP POST request and Bearer authorization.

Request parameters

Let’s take a look at some of the important parameters for our request, their descriptions, and their formats in the table below.

Parameter Name

Description

Format

Required

path

This is the path in Dropbox where this folder is created.

String

Yes

autorename

If this parameter is set to True and the name of the folder already exists in that path, the Dropbox server will then try to automatically rename the folder in order to avoid conflicts.

Boolean

No

Press + to interact
import requests
import json
url = "https://api.dropboxapi.com/2/files/create_folder_v2"
headers = {
"Authorization": "Bearer {{TOKEN}}",
"Content-Type": "application/json"
}
data = {
"path": "/home/TestFolder",
"autorename": True
}
r = requests.request("POST",url, headers=headers, data=json.dumps(data))
print(json.dumps(r.json(), indent=4))

In the code above, we see the following:

  • Lines 11–14: We define the data object that’s being passed to the endpoint. It includes a path with the value /home/TestFolder, which specifies both the directory and the name of our folder. This means that our TestFolder will be created in the home directory.
  • Line 13: We define the autorename value, which is set to True. If the home directory already has a file or a folder with the same name, then Dropbox will automatically rename our new folder.
  • Line 16: We make the HTTP request.
  • Line 17: We print the response.

Response fields

The code above will return the metadata of our new folder. This includes the name, path_display, path_lower, and id properties.

Upload a file

The /files/upload endpoint creates a file in the user’s Dropbox at the specified location with the content of the incoming file. It requires us to enable the files.content.write permission. We can upload any file within the size limit of 150 MB.

Note: When you test this endpoint on Educative’s platform, only files with a .png extension can be uploaded.

Request parameters

Let’s take a look at some of the important parameters for our request, their descriptions, and their formats in the table below.

Parameter Name

Description

Format

Required

file to upload

The file we want to upload will be given in the data object.

---

Yes

path

This parameter defines where we want to upload our file. We also need to give a name to the file in the path. The uploaded file will be available with this name in our Dropbox.

String

Yes

mute

Whenever changes are made to a file, the user is notified. However, if mute is set to True, then this functionality is disabled. The default value is False.

Boolean

No

Press + to interact
import requests
import json
url = "https://content.dropboxapi.com/2/files/upload"
headers = {
"Authorization": "Bearer {{TOKEN}}",
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": "{\"path\":\"/home/TestFolder/demo.png\",\"mute\":false}"
}
data = open("/usercode/__ed_input.png", "rb").read()
r = requests.request("POST",url, headers=headers, data=data)
print(json.dumps(r.json(), indent=4))

When we click “Run,” we’ll be asked to upload an image in .png format. The image size should be 1024 x 512px. If we add an image larger than this size, we’ll have to crop it before we continue.

In the code above, we see the following:

  • Line 4: This shows the URL for this endpoint. Here, we see that this call’s base URL is different from the previous ones we’ve used. It has the base URL content.dropboxapi.com instead of api.dropboxapi.com.
  • Lines 6–10: We define the headers of the request and for this endpoint.
  • Line 8: We indicate the value of Content-Type. For this endpoint, it’s application/octet-stream, and it’s used to open our file in an application.
  • Line 9: We list the arguments for the call. We’ve specified the /home/TestFolder/ as the destination path to our file and demo.png as its name in Dropbox. The content of our uploaded file will go inside it and set the values of mute to False.
  • Line 12: We open and read the input file __ed_input.png and assign it to the data parameter. The rb flag shows that this file will be opened in binary form and read mode.
  • Line 14: We define the POST request.
  • Line 15: We print the response.

Response fields

Let’s see what we get in response to the successful execution of the upload endpoint. Some of the response fields are listed in the table below:

Response field

Description

Format

name

This is the file name, which is the same as the one we defined in the path parameter.

String

path_display

This is the case-insensitive path to the file.

String

id

This is the file’s unique ID.

String

size

This is the size of the file in bytes.

UInt64

is_downloadable

If this is set to True, then the file can be downloaded directly. It’s set to True by default.

Boolean