0
Add a user to a team
One script reply has been approved by the moderators Verified

The user making this call must be a member of the team in order to add others. The user being added must exist in the same organization as the team.

Returns the complete team membership record for the newly added user.

Created by hugo697 418 days ago Viewed 9163 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 418 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Add a user to a team
6
 * The user making this call must be a member of the team in order to add others. The user being added must exist in the same organization as the team.
7

8
Returns the complete team membership record for the newly added user.
9
 */
10
export async function main(
11
  auth: Asana,
12
  team_gid: string,
13
  opt_pretty: string | undefined,
14
  opt_fields: string | undefined,
15
  body: { data?: { user?: string; [k: string]: unknown }; [k: string]: unknown }
16
) {
17
  const url = new URL(
18
    `https://app.asana.com/api/1.0/teams/${team_gid}/addUser`
19
  );
20
  for (const [k, v] of [
21
    ["opt_pretty", opt_pretty],
22
    ["opt_fields", opt_fields],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "POST",
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