0
Add users to a project
One script reply has been approved by the moderators Verified

Adds the specified list of users as members of the project. Note that a user being added as a member may also be added as a follower as a result of this operation. This is because the user's default notification settings (i.e., in the "Notifcations" tab of "My Profile Settings") will override this endpoint's default behavior of setting "Tasks added" notifications to false. Returns the updated project record.

Created by hugo697 313 days ago Viewed 8960 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 313 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Add users to a project
6
 * Adds the specified list of users as members of the project. Note that a user being added as a member may also be added as a *follower* as a result of this operation. This is because the user's default notification settings (i.e., in the "Notifcations" tab of "My Profile Settings") will override this endpoint's default behavior of setting "Tasks added" notifications to `false`.
7
Returns the updated project record.
8
 */
9
export async function main(
10
  auth: Asana,
11
  project_gid: string,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  body: {
15
    data?: { members: string; [k: string]: unknown };
16
    [k: string]: unknown;
17
  }
18
) {
19
  const url = new URL(
20
    `https://app.asana.com/api/1.0/projects/${project_gid}/addMembers`
21
  );
22
  for (const [k, v] of [
23
    ["opt_pretty", opt_pretty],
24
    ["opt_fields", opt_fields],
25
  ]) {
26
    if (v !== undefined && v !== "") {
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