0

Restore file

by
Published Oct 17, 2025

Restores a file that has been moved to the trash. An optional new parent ID can be provided to restore the file to in case the original folder has been deleted.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Restore file
7
 * Restores a file that has been moved to the trash.
8

9
An optional new parent ID can be provided to restore the file to in case the
10
original folder has been deleted.
11
 */
12
export async function main(
13
  auth: Box,
14
  file_id: string,
15
  fields: string | undefined,
16
  body: { name?: string; parent?: { id?: string } & {} },
17
) {
18
  const url = new URL(`https://api.box.com/2.0/files/${file_id}`);
19
  for (const [k, v] of [["fields", fields]]) {
20
    if (v !== undefined && v !== "" && k !== undefined) {
21
      url.searchParams.append(k, v);
22
    }
23
  }
24
  const response = await fetch(url, {
25
    method: "POST",
26
    headers: {
27
      "Content-Type": "application/json",
28
      Authorization: "Bearer " + auth.token,
29
    },
30
    body: JSON.stringify(body),
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38