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_v2
upload
copy_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 |
// Define import libraries hereimport fetch from 'node-fetch';// Define API key hereconst token = '{{TOKEN}}'// Define endpoint URL hereconst endpointUrl = new URL('https://api.dropboxapi.com/2/files/create_folder_v2');// Define Header Parameters hereconst headerParameters = {'Authorization': `Bearer ${token}`,'Content-Type': 'application/json',};// Define Body Parameters hereconst bodyParameters = JSON.stringify({path: "/home/TestFolder",autorename: true});// Set the API call optionsconst options = {method: 'POST',headers: headerParameters,body: bodyParameters,};// Function to make the API callasync function createFolder() {try {const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Call function to make the API callcreateFolder();
In the code above, we do the following:
- Line 2: We import the
fetch
library that we need to use in our code fromnode-fetch
. - Line 5: We get the value for
TOKEN
to use for authentication. - Line 8: We define the request URL.
- Lines 11–14: We define the
headers
of thefetch
request. It includes the authorization and the type of content, which isapplication/json
in most cases. - Lines 17–20: We define the
bodyParameters
of thefetch
request. These include thepath
of the directory where the new folder will be created and theautorename
option, 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
try
to get the response and will pass the response to the custom functionprintResponse
; otherwise, it willcatch
the error and will pass it to the custom functionprintError
. - Line 43: We call the function
createFolder
to 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
.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 | 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 |
// Define import libraries hereimport fetch from 'node-fetch';import fs from 'fs';// Define API key hereconst token = '{{TOKEN}}'// Define endpoint URL hereconst endpointUrl = new URL('https://content.dropboxapi.com/2/files/upload');// Define Header Parameters hereconst headerParameters = {'Authorization': `Bearer ${token}`,'Content-Type': 'application/octet-stream',"Dropbox-API-Arg": "{\"path\":\"/home/TestFolder/demo.png\",\"mute\":false}"};//Read the Image to uploadconst data =fs.readFileSync('__ed_input.png');// Set the API call optionsconst options = {method: 'POST',headers: headerParameters,body : data};// Function to make the API callasync function uploadFile() {try {const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Call function to make the API calluploadFile();
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
fetch
library that we need to use in our code fromnode-fetch
and importfs
. - Line 6: We get the value for
TOKEN
to use for authentication. - Line 9: We define the request URL.
- Lines 12–16: We define the
headers
of thefetch
request. It includes the authorization and the type of content, which isapplication/octet-stream
in 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
try
to get the response and will pass the response to the custom functionprintResponse
otherwise it willcatch
the error and will pass to the custom functionprintError
. - Line 42: We call the function
uploadFile
to 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
TestFolder
that 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 |
// Define import libraries hereimport fetch from 'node-fetch';// Define API key hereconst token = '{{TOKEN}}'// Define endpoint URL hereconst endpointUrl = new URL('https://api.dropboxapi.com/2/files/copy_v2');// Define Header Parameters hereconst headerParameters = {'Authorization': `Bearer ${token}`,'Content-Type': 'application/json',};// Define Body Parameters hereconst bodyParameters = JSON.stringify({from_path: "/home/TestFolder",to_path: "/home/DestFolder",autorename: true});// Set the API call optionsconst options = {method: 'POST',headers: headerParameters,body: bodyParameters,};// Function to make the API callasync function copyFiles() {try {const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Call function to make the API callcopyFiles();
In the code above, we do the following:
- Line 2: We import the
fetch
library that we need to use in our code fromnode-fetch
. - Line 5: We get the value for
TOKEN
to use for authentication. - Line 8: We define the request URL.
- Lines 11–14: We define the
headers
of thefetch
request. It includes the authorization and the type of content, which isapplication/json
in most cases. - Lines 17–21: We define the
bodyParameters
of thefetch
request. Thefrom_path
parameter defines the source of files that we need to copy, andto_path
defines the path of the destination folder of those copied files. Theauto_rename
parameter 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
try
to get the response and will pass the response to the custom functionprintResponse
; otherwise, it willcatch
the error and will pass it to the custom functionprintError
. - Line 44: We call the function
copyFiles
to 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.