0

Download file

by
Published Oct 17, 2025

Returns the contents of a file in binary format.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Download file
7
 * Returns the contents of a file in binary format.
8
 */
9
export async function main(
10
  auth: Box,
11
  file_id: string,
12
  version: string | undefined,
13
  access_token: string | undefined,
14
  range: string,
15
  boxapi: string,
16
) {
17
  const url = new URL(`https://api.box.com/2.0/files/${file_id}/content`);
18
  for (const [k, v] of [
19
    ["version", version],
20
    ["access_token", access_token],
21
  ]) {
22
    if (v !== undefined && v !== "" && k !== undefined) {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      range: range,
30
      boxapi: boxapi,
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41