0

Add shared link to file

by
Published Oct 17, 2025

Adds a shared link to a file.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Add shared link to file
7
 * Adds a shared link to a file.
8
 */
9
export async function main(
10
  auth: Box,
11
  file_id: string,
12
  fields: string | undefined,
13
  body: {
14
    shared_link?: {
15
      access?: "open" | "company" | "collaborators";
16
      password?: string;
17
      vanity_name?: string;
18
      unshared_at?: string;
19
      permissions?: {
20
        can_download?: false | true;
21
        can_preview?: false | true;
22
        can_edit?: false | true;
23
      };
24
    };
25
  },
26
) {
27
  const url = new URL(
28
    `https://api.box.com/2.0/files/${file_id}#add_shared_link`,
29
  );
30
  for (const [k, v] of [["fields", fields]]) {
31
    if (v !== undefined && v !== "" && k !== undefined) {
32
      url.searchParams.append(k, v);
33
    }
34
  }
35
  const response = await fetch(url, {
36
    method: "PUT",
37
    headers: {
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49