0

Restore folder

by
Published Oct 17, 2025

Restores a folder that has been moved to the trash. An optional new parent ID can be provided to restore the folder to in case the original folder has been deleted. During this operation, part of the file tree will be locked, mainly the source folder and all of its descendants, as well as the destination folder. For the duration of the operation, no other move, copy, delete, or restore operation can performed on any of the locked folders.

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 folder
7
 * Restores a folder that has been moved to the trash.
8

9
An optional new parent ID can be provided to restore the folder to in case the
10
original folder has been deleted.
11

12
During this operation, part of the file tree will be locked, mainly
13
the source folder and all of its descendants, as well as the destination
14
folder.
15

16
For the duration of the operation, no other move, copy, delete, or restore
17
operation can performed on any of the locked folders.
18
 */
19
export async function main(
20
  auth: Box,
21
  folder_id: string,
22
  fields: string | undefined,
23
  body: { name?: string; parent?: { id?: string } & {} },
24
) {
25
  const url = new URL(`https://api.box.com/2.0/folders/${folder_id}`);
26
  for (const [k, v] of [["fields", fields]]) {
27
    if (v !== undefined && v !== "" && k !== undefined) {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Content-Type": "application/json",
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: JSON.stringify(body),
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45