Remove a Folder Member and Unshare it
Learn how to remove a folder member and unshare a folder using the Dropbox API.
We'll cover the following
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 |
| String | Yes | Unique ID of the shared folder. |
|
| Yes | Defines which member should be removed. They can be specified with their |
| 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.
// 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/sharing/remove_folder_member');// Define Header Parameters hereconst headerParameters = {'Authorization': `Bearer ${token}`,'Content-Type': 'application/json',};// Define Body Parameters hereconst bodyParameters = JSON.stringify({shared_folder_id: "{{FOLDER_ID}}",member: {".tag":"email","email":"user_id@gmail.com"},leave_a_copy: false});// Set the API call optionsconst options = {method: 'POST',headers: headerParameters,body: bodyParameters,};// Function to make the API callasync function removeMember() {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 callremoveMember();
In the code above, we see 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–23: We define the
bodyParameters
of thefetch
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 functionprintResponse
; otherwise, it willcatch
the error and will pass it to the custom functionprintError
. - 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.
// 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/sharing/check_job_status');// Define Header Parameters hereconst headerParameters = {'Authorization': `Bearer ${token}`,'Content-Type': 'application/json',};// Define Body Parameters hereconst bodyParameters = JSON.stringify({async_job_id: "{async_job_id}"});// Set the API call optionsconst options = {method: 'POST',headers: headerParameters,body: bodyParameters,};// Function to make the API callasync function fetchSomethingJSON() {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 callfetchSomethingJSON();
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 |
| String | Yes | Unique ID of the shared folder that we obtained in the previous lesson. |
| Boolean | No | If it’s set to |
The following code unshares a shared folder:
// 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/sharing/unshare_folder');// Define Header Parameters hereconst headerParameters = {'Authorization': `Bearer ${token}`,'Content-Type': 'application/json',};// Define Body Parameters hereconst bodyParameters = JSON.stringify({shared_folder_id: "{{FOLDER_ID}}"});// Set the API call optionsconst options = {method: 'POST',headers: headerParameters,body: bodyParameters,};// Function to make the API callasync function unshareFolder() {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 callunshareFolder();
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–19: We define the
bodyParameters
of thefetch
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 functionprintResponse
; otherwise, it willcatch
the error and will pass it to the custom functionprintError
. - 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.
Note: After getting the
complete
status of the above asynchronous job, visit thepreview_url
again in the browser. You’ll notice that the folder’s shareable link is no longer valid.