0

List all file versions

by
Published Oct 17, 2025

Retrieve a list of the past versions for a file. Versions are only tracked by Box users with premium accounts. To fetch the ID of the current version of a file, use the `GET /file/:id` API.

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 all file versions
7
 * Retrieve a list of the past versions for a file.
8

9
Versions are only tracked by Box users with premium accounts. To fetch the ID
10
of the current version of a file, use the `GET /file/:id` API.
11
 */
12
export async function main(
13
  auth: Box,
14
  file_id: string,
15
  fields: string | undefined,
16
  limit: string | undefined,
17
  offset: string | undefined,
18
) {
19
  const url = new URL(`https://api.box.com/2.0/files/${file_id}/versions`);
20
  for (const [k, v] of [
21
    ["fields", fields],
22
    ["limit", limit],
23
    ["offset", offset],
24
  ]) {
25
    if (v !== undefined && v !== "" && k !== undefined) {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42