0

Add user to group

by
Published Oct 17, 2025

Creates a group membership. Only users with admin-level permissions will be able to use this API.

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 user to group
7
 * Creates a group membership. Only users with
8
admin-level permissions will be able to use this API.
9
 */
10
export async function main(
11
  auth: Box,
12
  fields: string | undefined,
13
  body: {
14
    user: { id: string };
15
    group: { id: string };
16
    role?: "member" | "admin";
17
    configurable_permissions?: {};
18
  },
19
) {
20
  const url = new URL(`https://api.box.com/2.0/group_memberships`);
21
  for (const [k, v] of [["fields", fields]]) {
22
    if (v !== undefined && v !== "" && k !== undefined) {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "POST",
28
    headers: {
29
      "Content-Type": "application/json",
30
      Authorization: "Bearer " + auth.token,
31
    },
32
    body: JSON.stringify(body),
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40