Create, List, and Get File Requests

Learn how to create, list, and get the total number of file requests.

In this lesson, we’ll see some important endpoints of the file_requests namespace. These endpoints require us to enable the file_requests.read and file_requests.write permissions. We’ll discuss the following endpoint operations:

  • create
  • list
  • count

Create a file request

We’ll first explore the /file_requests/create endpoint. It initiates a request to create a file for the user whose access token is being passed in the request’s headers parameter. This is an HTTP POST request.

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

title

String

Yes

Specifies the name of the requested file.

destination

String

Yes

Defines the path to the Dropbox folder where we want to create or save a file. Path should be in "/(.|[\r\n])*" format. Applications that have enabled the “App folder” permission will have a path relative to the specified folder.

deadline

Object

No

Can only be used by professional and business accounts.

open

Boolean

No

Indicates whether this file should be open or not. Submissions can’t be made if the file is closed. It’s set to true by default.

description

String

No

Defines the description of the file request.

Let’s take a look at the code for this endpoint below. When we execute the code, the file ID is automatically extracted. We’ll need this file ID in the next lesson, so let’s click the “Save” button to save it.

Press + to interact
// Define import libraries here
import fetch from 'node-fetch';
// Define API key here
const token = '{{TOKEN}}'
// Define endpoint URL here
const endpointUrl = new URL('https://api.dropboxapi.com/2/file_requests/create');
// Define Header Parameters here
const headerParameters = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
};
// Define Body Parameters here
const bodyParameters = JSON.stringify({
title : "demoFileByAPI",
destination : "/home",
open : true,
description : "We used an API call to create this request."
});
// Set the API call options
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
// Function to make the API call
async function fetchRequest() {
try {
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Call function to make the API call
fetchRequest();

In the code above, we do the following:

  • Line 2: We import the fetch library that we need to use in our code from node-fetch.
  • Line 5: We get the value for TOKEN to use for authentication.
  • Line 8: We define the request URL.
  • Lines 11–13: We define the headers of the fetch request. It includes the authorization and the type of content, which is application/json in most cases.
  • Lines 17–22: We define the bodyParameters of the fetch request.
  • Lines 25–29: We set the API call options.
  • Lines 32–42: We create a function to make the API call. It will try to get the response and will pass the response to the custom function printResponse; otherwise, it will catch the error and will pass it to the custom function printError.
  • Line 45: We call the function fetchRequest to make the API call.

Response fields

We’ll now go to our Dropbox dashboard and select the “File requests” option from the side menu. Our file request will be available here.

Let’s take a look at some of the response fields in the following table:

Field

Format

Description

id

String

Unique ID of the file.

url

String

URL of our created file; anyone with this link can upload files to our Dropbox.

title

String

Title of the file that we specified in the call.

destination

String in format "/(.|[\r\n])*"

Provides the full path to the file (in our case, it’s "/home").

is_open

Boolean

Tells us the status of the file, if it’s open or closed. If the file is closed, then content can’t be uploaded to it.

List all file requests

Let’s list all the file requests for a specific user. The /file_requests/list endpoint requires us to enable the file_requests.read permission. There are no parameters required to make this call—let’s take a look at the code to see how we can write this request:

Press + to interact
// Define import libraries here
import fetch from 'node-fetch';
// Define API key here
const token = '{{TOKEN}}'
// Define endpoint URL here
const endpointUrl = new URL('https://api.dropboxapi.com/2/file_requests/list');
// Define Header Parameters here
const headerParameters = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
};
// Set the API call options
const options = {
method: 'POST',
headers: headerParameters,
body : 'null'
};
// Function to make the API call
async function fetchRequestList() {
try {
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Call function to make the API call
fetchRequestList();

In the code above, we do the following:

  • Line 2: We import the fetch library that we need to use in our code from node-fetch.
  • Line 5: We get the value for TOKEN to use for authentication.
  • Line 8: We define the request URL.
  • Lines 11–13: We define the headers of the fetch request. It includes the authorization and the type of content, which is application/json in most cases.
  • Lines 17–21: We set the API call options.
  • Lines 24–33: We create a function to make the API call. It will try to get the response and will pass the response to the custom function printResponse; otherwise, it will catch the error and will pass it to the custom function printError.
  • Line 36: We call the function fetchRequestList to make the API call.

The successful execution of the code above will return file_requests, including all the requested files and their properties.

Response fields

Most of the response fields are the same as the ones we’ve discussed previously. Regardless, let’s take a look at some of the important response fields for the list endpoint in the table below:

Fields

Format

Description

id

string

Unique ID of the file request.

is_open

boolean

Indicates whether the file request is open or closed.

file_count

int

Total number of files that were uploaded using the URL associated with the file request.

The file ID returned by the list endpoint can be used to get information about a single file request using the /file_requests/get endpoint. The file ID will be given in the data object.

Get the total number of file requests

The /file_requests/count endpoint of the file_requests namespace is very straightforward. It doesn’t take any parameters and returns the total number of file requests.

Press + to interact
// Define import libraries here
import fetch from 'node-fetch';
// Define API key here
const token = '{{TOKEN}}'
// Define endpoint URL here
const endpointUrl = new URL('https://api.dropboxapi.com/2/file_requests/count');
// Define Header Parameters here
const headerParameters = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
};
// Set the API call options
const options = {
method: 'POST',
headers: headerParameters,
body : 'null'
};
// Function to make the API call
async function fetchTotalRequests() {
try {
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Call function to make the API call
fetchTotalRequests();

In the code above, we do the following:

  • Line 2: We import the fetch library that we need to use in our code from node-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 the fetch request. It includes the authorization and the type of content, which is application/json in most cases.
  • Lines 17–21: We set the API call options.
  • Lines 24–33: We create a function to make the API call. It will try to get the response and will pass the response to the custom function printResponse; otherwise, it will catch the error and will pass it to the custom function printError.
  • Line 36: We call the function fetchTotalRequests to make the API call.

Response

The code above will return the total file requests specific to the current user.