0

Get current user

by
Published Oct 17, 2025

Retrieves information about the user who is currently authenticated. In the case of a client-side authenticated OAuth 2.0 application this will be the user who authorized the app. In the case of a JWT, server-side authenticated application this will be the service account that belongs to the application by default. Use the `As-User` header to change who this API call is made on behalf of.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Get current user
7
 * Retrieves information about the user who is currently authenticated.
8

9
In the case of a client-side authenticated OAuth 2.0 application
10
this will be the user who authorized the app.
11

12
In the case of a JWT, server-side authenticated application
13
this will be the service account that belongs to the application
14
by default.
15

16
Use the `As-User` header to change who this API call is made on behalf of.
17
 */
18
export async function main(auth: Box, fields: string | undefined) {
19
  const url = new URL(`https://api.box.com/2.0/users/me`);
20
  for (const [k, v] of [["fields", fields]]) {
21
    if (v !== undefined && v !== "" && k !== undefined) {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "GET",
27
    headers: {
28
      Authorization: "Bearer " + auth.token,
29
    },
30
    body: undefined,
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38