0

Transfer owned folders

by
Published Oct 17, 2025

Move all of the items (files, folders and workflows) owned by a user into another user's account Only the root folder (`0`) can be transferred.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Transfer owned folders
7
 * Move all of the items (files, folders and workflows) owned by a user into
8
another user's account
9

10
Only the root folder (`0`) can be transferred.
11
 */
12
export async function main(
13
  auth: Box,
14
  user_id: string,
15
  fields: string | undefined,
16
  notify: string | undefined,
17
  body: { owned_by: { id: string } },
18
) {
19
  const url = new URL(`https://api.box.com/2.0/users/${user_id}/folders/0`);
20
  for (const [k, v] of [
21
    ["fields", fields],
22
    ["notify", notify],
23
  ]) {
24
    if (v !== undefined && v !== "" && k !== undefined) {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "PUT",
30
    headers: {
31
      "Content-Type": "application/json",
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: JSON.stringify(body),
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42