Remove a Folder Member and Unshare it

Learn how to remove a folder member and unshare a folder using the Dropbox API.

In this lesson, we’ll explore some more endpoints of the sharing namespace, which include the following:

  • remove_folder_member
  • unshare_folder

Remove a folder member

The sharing/remove_folder_member endpoint is used to remove a specific member from a folder. Only the members with owner or editor rights can perform this task. This endpoint also needs us to enable the sharing.write permission.

Request parameters

It takes three required parameters, which are outlined in the following table:

Parameter Name

Format/Value

Required

Description

shared_folder_id

String

Yes

Unique ID of the shared folder.

member

email

Yes

Defines which member should be removed. They can be specified with their dropbox_id or email.

leave_a_copy

Boolean

Yes

Indicates whether the removed member will keep a copy of the unshared folder or not.

The code below will remove the folder member with the email user_id@gmail.com we added in the previous lesson.

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/sharing/remove_folder_member');
// Define Header Parameters here
const headerParameters = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
};
// Define Body Parameters here
const bodyParameters = JSON.stringify({
shared_folder_id: "{{FOLDER_ID}}",
member: {
".tag":"email",
"email":"user_id@gmail.com"},
leave_a_copy: false
});
// Set the API call options
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
// Function to make the API call
async function removeMember() {
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
removeMember();

In the code above, we see 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–23: We define the bodyParameters of the fetch request.
  • Lines 26–30: We set the API call options.
  • Lines 33–43: 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 46: We call the function removeMember to make the API call.

Response

This operation works in exactly the same way as share_folder in that it can be executed synchronously or asynchronously. With synchronous execution, our response is just a completion message. However, if it’s executed asynchronously, async_job_id will be returned in the response, and we’ll have to check the status of the operation like we did with share_folder and use the same endpoint.

If async_job_id is returned by the code, then we’ll use the check_job_status endpoint. We’ll replace the placeholder {async_job_id} in line 18 with the async_job_id returned by the previous block of code. It’ll produce a complete response once the operation is done.

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/sharing/check_job_status');
// Define Header Parameters here
const headerParameters = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
};
// Define Body Parameters here
const bodyParameters = JSON.stringify({
async_job_id: "{async_job_id}"
});
// Set the API call options
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
// Function to make the API call
async function fetchSomethingJSON() {
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
fetchSomethingJSON();

Unshare the folder

The /sharing/unshare_folder endpoint permits the folder members with owner rights to unshare the folder. This endpoint also requires us to have the permission sharing.write enabled.

Request parameters

This endpoint has one required and one optional parameter. Let’s take a look at them in the following table:

Parameter Name

Format

Required

Description

shared_folder_id

String

Yes

Unique ID of the shared folder that we obtained in the previous lesson.

leave_a_copy

Boolean

No

If it’s set to true, then all folder members will get a copy of the folder once it has been unshared. Otherwise, its default value is set to false.

The following code unshares a shared folder:

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/sharing/unshare_folder');
// Define Header Parameters here
const headerParameters = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
};
// Define Body Parameters here
const bodyParameters = JSON.stringify({
shared_folder_id: "{{FOLDER_ID}}"
});
// Set the API call options
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
// Function to make the API call
async function unshareFolder() {
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
unshareFolder();

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–19: We define the bodyParameters of the fetch request.
  • 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 function printResponse; otherwise, it will catch the error and will pass it to the custom function printError.
  • Line 42: We call the function unshareFolder to make the API call.

Response

This operation can work synchronously or asynchronously. If it’s synchronously executed, the response is just a completion message. If the execution is asynchronous, the response will be async_job_id, and we’ll have to check the status of the operation by executing the check_job_status endpoint from the previous section.

Press + to interact
Empty shared folder
Empty shared folder

Note: After getting the complete status of the above asynchronous job, visit the preview_url again in the browser. You’ll notice that the folder’s shareable link is no longer valid.

Preview URL of the SharedFolder