0

Add a Member to a Board

by
Published Oct 30, 2023

Add a member to the board.

Script trello Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Trello = {
2
  key: string;
3
  token: string;
4
};
5
/**
6
 * Add a Member to a Board
7
 * Add a member to the board.
8
 */
9
export async function main(
10
  auth: Trello,
11
  id: string,
12
  idMember: string,
13
  type: "admin" | "normal" | "observer" | undefined,
14
  allowBillableGuest: string | undefined
15
) {
16
  const url = new URL(
17
    `https://api.trello.com/1/boards/${id}/members/${idMember}`
18
  );
19
  for (const [k, v] of [
20
    ["type", type],
21
    ["allowBillableGuest", allowBillableGuest],
22
    ["key", auth.key],
23
    ["token", auth.token],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "PUT",
31
    headers: {
32
      Authorization: undefined,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.text();
41
}
42