0

Get file thumbnail

by
Published Oct 17, 2025

Retrieves a thumbnail, or smaller image representation, of a file. Sizes of `32x32`,`64x64`, `128x128`, and `256x256` can be returned in the `.png` format and sizes of `32x32`, `160x160`, and `320x320` can be returned in the `.jpg` format. Thumbnails can be generated for the image and video file formats listed [found on our community site][1]. [1]: https://community.box.com/t5/Migrating-and-Previewing-Content/File-Types-and-Fonts-Supported-in-Box-Content-Preview/ta-p/327

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 file thumbnail
7
 * Retrieves a thumbnail, or smaller image representation, of a file.
8

9
Sizes of `32x32`,`64x64`, `128x128`, and `256x256` can be returned in
10
the `.png` format and sizes of `32x32`, `160x160`, and `320x320`
11
can be returned in the `.jpg` format.
12

13
Thumbnails can be generated for the image and video file formats listed
14
[found on our community site][1].
15

16
[1]: https://community.box.com/t5/Migrating-and-Previewing-Content/File-Types-and-Fonts-Supported-in-Box-Content-Preview/ta-p/327
17
 */
18
export async function main(
19
  auth: Box,
20
  file_id: string,
21
  extension: "png" | "jpg",
22
  min_height: string | undefined,
23
  min_width: string | undefined,
24
  max_height: string | undefined,
25
  max_width: string | undefined,
26
) {
27
  const url = new URL(
28
    `https://api.box.com/2.0/files/${file_id}/thumbnail.${extension}`,
29
  );
30
  for (const [k, v] of [
31
    ["min_height", min_height],
32
    ["min_width", min_width],
33
    ["max_height", max_height],
34
    ["max_width", max_width],
35
  ]) {
36
    if (v !== undefined && v !== "" && k !== undefined) {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53