0

Add a user to a workspace or organization

by
Published Oct 31, 2023

Add a user to a workspace or organization. The user can be referenced by their globally unique user ID or their email address. Returns the full user record for the invited user.

Script asana Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Add a user to a workspace or organization
6
 * Add a user to a workspace or organization.
7
The user can be referenced by their globally unique user ID or their email address. Returns the full user record for the invited user.
8
 */
9
export async function main(
10
  auth: Asana,
11
  workspace_gid: string,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  body: { data?: { user?: string; [k: string]: unknown }; [k: string]: unknown }
15
) {
16
  const url = new URL(
17
    `https://app.asana.com/api/1.0/workspaces/${workspace_gid}/addUser`
18
  );
19
  for (const [k, v] of [
20
    ["opt_pretty", opt_pretty],
21
    ["opt_fields", opt_fields],
22
  ]) {
23
    if (v !== undefined && v !== "") {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      "Content-Type": "application/json",
31
      Authorization: "Bearer " + auth.token,
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.json();
40
}
41