0

Adds a new member to a project.

by
Published Apr 8, 2025

Adds a new member to the project.

Script vercel Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Vercel = {
3
  token: string;
4
};
5
/**
6
 * Adds a new member to a project.
7
 * Adds a new member to the project.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  idOrName: string,
12
  teamId: string | undefined,
13
  slug: string | undefined,
14
  body: {
15
    uid?: string;
16
    username?: string;
17
    email?: string;
18
    role: "ADMIN" | "PROJECT_DEVELOPER" | "PROJECT_VIEWER";
19
  } & {},
20
) {
21
  const url = new URL(`https://api.vercel.com/v1/projects/${idOrName}/members`);
22
  for (const [k, v] of [
23
    ["teamId", teamId],
24
    ["slug", slug],
25
  ]) {
26
    if (v !== undefined && v !== "" && k !== undefined) {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "Content-Type": "application/json",
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44