Create a Folder, Upload, and Copy Files
Learn how to create a new folder and upload and copy 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 three endpoint operations:
create_folder_v2uploadcopy_v2
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 | Format | Required | Description |
| String | Yes | The path in Dropbox where this folder is created. |
| Boolean | No | If this parameter is set to |
In the code above, we do the following:
- Line 2: We import the
fetchlibrary that we need to use in our code fromnode-fetch. - Line 5: We get the value for
TOKENto use for authentication. - Line 8: We define the request URL.
- Lines 11–14: We define the
headersof thefetchrequest. It includes the authorization and the type of content, which isapplication/jsonin most cases. - Lines 17–20: We define the
bodyParametersof thefetchrequest. These include thepathof the directory where the new folder will be created and theautorenameoption, which specifies if that new folder can have a suffix in its name if a folder with the same name already exists. - Lines 23–27: We create a function to make the API call. It will
tryto get the response and will pass the response to the custom functionprintResponse; otherwise, it willcatchthe error and will pass it to the custom functionprintError. - Line 43: We call the function
createFolderto make the API call.
Response fields
The code above will return the metadata of our new folder. This includes the name, path_display, path_lower, and id properties.
Response field | Format | Description |
| String | Name of the folder that is created. |
| String | Case-insensitive path to the folder. |
| String | Lower-case path to the folder. |
| String | Folder’s unique ID. |
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
.pngextension 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 | Format | Required | Description |
file to upload | --- | Yes | The file we want to upload will be given in the |
| String | Yes | 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. |
| Boolean | No | Whenever changes are made to a file, the user is notified. If |
When we click “Run,” we’ll be asked to upload an image in .png format. The image size should be 1024 x 512 px. If we add an image larger than this size, we’ll have to crop it before we can continue.
In the code above, we do the following:
- Lines 2–3: We import the
fetchlibrary that we need to use in our code fromnode-fetchand importfs. - Line 6: We get the value for
TOKENto use for authentication. - Line 9: We define the request URL.
- Lines 12–16: We define the
headersof thefetchrequest. It includes the authorization and the type of content, which isapplication/octet-streamin this case. - Line 19: We read the image to upload.
- Lines 22–26: We set the API call options.
- Lines 29–39: We create a function to make the API call. It will
tryto get the response and will pass the response to the custom functionprintResponseotherwise it willcatchthe error and will pass to the custom functionprintError. - Line 42: We call the function
uploadFileto make the API call.
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 | Format | Description |
Boolean | No | Set to |
| String | Case-insensitive path to the file. |
| String | File’s unique ID. |
| Integer | Size of the file in bytes. |
| Boolean | If this is set to |
Copy a file or a folder
The /files/copy_v2 endpoint lets us copy a file or folder to another location in Dropbox. If the specified source path is a folder, then all of its content will be copied. This endpoint requires us to enable the permission of the files.content.write.
Request parameters
Let’s see some of the parameters for this call in the table below.
Note: We’ve manually added some files to the
TestFolderthat we created in the previous lesson so that we may copy them to another folder.
Parameter Name | Format | Required | Description |
| String | Yes | The path to the Dropbox folder or file that we want to copy. |
| String | Yes | Destination path to the user's Dropbox. |
| This is set to | No | Set to |
In the code above, we do the following:
- Line 2: We import the
fetchlibrary that we need to use in our code fromnode-fetch. - Line 5: We get the value for
TOKENto use for authentication. - Line 8: We define the request URL.
- Lines 11–14: We define the
headersof thefetchrequest. It includes the authorization and the type of content, which isapplication/jsonin most cases. - Lines 17–21: We define the
bodyParametersof thefetchrequest. Thefrom_pathparameter defines the source of files that we need to copy, andto_pathdefines the path of the destination folder of those copied files. Theauto_renameparameter allows us to add a suffix to the name of files if any file is already available in the destination folder with the same name. - Lines 24–28: We set the API call options.
- Lines 31–41: We create a function to make the API call. It will
tryto get the response and will pass the response to the custom functionprintResponse; otherwise, it willcatchthe error and will pass it to the custom functionprintError. - Line 44: We call the function
copyFilesto make the API call.
Response fields
In response to the execution of the code above, we’ll get the .tag, name, path_lower, path_display, and id of our folder or file. Now, in our Dropbox account, we’ll see a new folder was created at the specified path and the content was copied into it.