0

Update file

by
Published Oct 17, 2025

Updates a file. This can be used to rename or move a file, create a shared link, or lock 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
 * Update file
7
 * Updates a file. This can be used to rename or move a file,
8
create a shared link, or lock a file.
9
 */
10
export async function main(
11
  auth: Box,
12
  file_id: string,
13
  fields: string | undefined,
14
  if_match: string,
15
  body: {
16
    name?: string;
17
    description?: string;
18
    parent?: { id?: string; user_id?: string } & {};
19
    shared_link?: {
20
      access?: "open" | "company" | "collaborators";
21
      password?: string;
22
      vanity_name?: string;
23
      unshared_at?: string;
24
      permissions?: { can_download?: false | true };
25
    } & {};
26
    lock?: {
27
      access?: "lock";
28
      expires_at?: string;
29
      is_download_prevented?: false | true;
30
    };
31
    disposition_at?: string;
32
    permissions?: { can_download?: "open" | "company" };
33
    collections?: { id?: string; type?: string }[];
34
    tags?: string[];
35
  },
36
) {
37
  const url = new URL(`https://api.box.com/2.0/files/${file_id}`);
38
  for (const [k, v] of [["fields", fields]]) {
39
    if (v !== undefined && v !== "" && k !== undefined) {
40
      url.searchParams.append(k, v);
41
    }
42
  }
43
  const response = await fetch(url, {
44
    method: "PUT",
45
    headers: {
46
      "if-match": if_match,
47
      "Content-Type": "application/json",
48
      Authorization: "Bearer " + auth.token,
49
    },
50
    body: JSON.stringify(body),
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