Create a Folder and Upload a File
Learn how to create a new folder and upload a file using the Dropbox API.
We'll cover the following
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 |
| This is the path in Dropbox where this folder is created. | String | Yes |
| If this parameter is set to | Boolean | No |
import requestsimport jsonurl = "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 ourTestFolder
will be created in thehome
directory. - Line 13: We define the
autorename
value, which is set toTrue
. If thehome
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 | --- | Yes |
| 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 |
| Whenever changes are made to a file, the user is notified. However, if | Boolean | No |
import requestsimport jsonurl = "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 ofapi.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’sapplication/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 anddemo.png
as its name in Dropbox. The content of our uploaded file will go inside it and set the values ofmute
toFalse
. - Line 12: We open and read the input file
__ed_input.png
and assign it to thedata
parameter. Therb
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 |
| This is the file name, which is the same as the one we defined in the | String |
| This is the case-insensitive path to the file. | String |
| This is the file’s unique ID. | String |
| This is the size of the file in bytes. | UInt64 |
| If this is set to | Boolean |