User Information

Learn how to get user information, such as account detail and space usage, using Dropbox API.

In this lesson, we’ll explore some of the important endpoints of the users namespace. These endpoints require us to enable the account_info.read permission. We’ll interact with the following endpoints in this lesson:

  • get_current_account
  • get_space_usage

Note: We granted permission for account_info.read in the previous lesson after we generated our access token.

Get current account details

The users namespace offers the following endpoint:
/users/get_current_account.
We can use this endpoint to retrieve a user’s information with the access token. This endpoint requires Bearer authorization, which is defined in the header of the HTTP request. It utilizes an HTTP POST request and doesn’t take any parameters.

Let’s see this endpoint’s functionality in the code below:

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/users/get_current_account');
// Define Header Parameters here
const headerParameters = {
authorization: `Bearer ${token}`,
contentType: 'application/json',
};
// Set the API call options
const options = {
method: 'POST',
headers: headerParameters,
};
// Function to make the API call
async function fetchAccount() {
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
fetchAccount();

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–20: We set the API call options.
  • Lines 23–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 pass it to the custom function printError.
  • Line 36: We call the function fetchAccount to make the API call.

Response fields

Some of the important fields from the response are shown in the table below.

Fields

Format

Description

account_id

String

Unique ID of the Dropbox account associated with the access token.

name

Object

Details of the user's name.

email

String

User’s email address.

email_verified

Boolean

Indicates if the user's email address has been verified.

profile_photo_url

String

URL (link) of the user's profile photo (if one has been uploaded).

Get details about space usage

The endpoint /users/get_space_usage doesn’t take any parameters and returns the status of the storage space allocated to and used by the user. Take a look at the code below to get more information about the user’s space usage:

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/users/get_space_usage');
// Define Header Parameters here
const headerParameters = {
authorization: `Bearer ${token}`,
contentType: 'application/json',
};
// Set the API call options
const options = {
method: 'POST',
headers: headerParameters,
};
// Function to make the API call
async function fetchStorage() {
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
fetchStorage();

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–20: We set the API call options.
  • Lines 23–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 fetchStorage to make the API call.

Response fields

Let’s take look at the various response fields for our space usage query in the following table:

Field

Format

Description

used

UInt64

Total amount of storage space used by the user (specified in bytes).

allocation

Object

Total storage space quota allocated to the user. Space allocation consists of two types, individual and team (in our case, it’s individual).