0

List user and enterprise events

by
Published Oct 17, 2025

Returns up to a year of past events for a given user or for the entire enterprise. By default this returns events for the authenticated user. To retrieve events for the entire enterprise, set the `stream_type` to `admin_logs_streaming` for live monitoring of new events, or `admin_logs` for querying across historical events. The user making the API call will need to have admin privileges, and the application will need to have the scope `manage enterprise properties` checked.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * List user and enterprise events
7
 * Returns up to a year of past events for a given user
8
or for the entire enterprise.
9

10
By default this returns events for the authenticated user. To retrieve events
11
for the entire enterprise, set the `stream_type` to `admin_logs_streaming`
12
for live monitoring of new events, or `admin_logs` for querying across
13
historical events. The user making the API call will
14
need to have admin privileges, and the application will need to have the
15
scope `manage enterprise properties` checked.
16
 */
17
export async function main(
18
  auth: Box,
19
  stream_type:
20
    | "all"
21
    | "changes"
22
    | "sync"
23
    | "admin_logs"
24
    | "admin_logs_streaming"
25
    | undefined,
26
  stream_position: string | undefined,
27
  limit: string | undefined,
28
  event_type: string | undefined,
29
  created_after: string | undefined,
30
  created_before: string | undefined,
31
) {
32
  const url = new URL(`https://api.box.com/2.0/events`);
33
  for (const [k, v] of [
34
    ["stream_type", stream_type],
35
    ["stream_position", stream_position],
36
    ["limit", limit],
37
    ["event_type", event_type],
38
    ["created_after", created_after],
39
    ["created_before", created_before],
40
  ]) {
41
    if (v !== undefined && v !== "" && k !== undefined) {
42
      url.searchParams.append(k, v);
43
    }
44
  }
45
  const response = await fetch(url, {
46
    method: "GET",
47
    headers: {
48
      Authorization: "Bearer " + auth.token,
49
    },
50
    body: undefined,
51
  });
52
  if (!response.ok) {
53
    const text = await response.text();
54
    throw new Error(`${response.status} ${text}`);
55
  }
56
  return await response.json();
57
}
58