0

Invite Member to Board via email

by
Published Oct 30, 2023

Invite a Member to a Board via their email address.

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
 * Invite Member to Board via email
7
 * Invite a Member to a Board via their email address.
8
 */
9
export async function main(
10
  auth: Trello,
11
  id: string,
12
  email: string | undefined,
13
  type: "admin" | "normal" | "observer" | undefined,
14
  body: { fullName?: string; [k: string]: unknown }
15
) {
16
  const url = new URL(`https://api.trello.com/1/boards/${id}/members`);
17
  for (const [k, v] of [
18
    ["email", email],
19
    ["type", type],
20
    ["key", auth.key],
21
    ["token", auth.token],
22
  ]) {
23
    if (v !== undefined && v !== "") {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "PUT",
29
    headers: {
30
      "Content-Type": "application/json",
31
      Authorization: undefined,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.text();
40
}
41