Get multiple users

Returns the user records for all users in all workspaces and organizations accessible to the authenticated user. Accepts an optional workspace ID parameter. Results are sorted by user ID.

Script asana Verified

by hugo697 ยท 10/31/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Get multiple users
6
 * Returns the user records for all users in all workspaces and organizations accessible to the authenticated user. Accepts an optional workspace ID parameter.
7
Results are sorted by user ID.
8
 */
9
export async function main(
10
  auth: Asana,
11
  workspace: string | undefined,
12
  team: string | undefined,
13
  opt_pretty: string | undefined,
14
  opt_fields: string | undefined,
15
  limit: string | undefined,
16
  offset: string | undefined
17
) {
18
  const url = new URL(`https://app.asana.com/api/1.0/users`);
19
  for (const [k, v] of [
20
    ["workspace", workspace],
21
    ["team", team],
22
    ["opt_pretty", opt_pretty],
23
    ["opt_fields", opt_fields],
24
    ["limit", limit],
25
    ["offset", offset],
26
  ]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: undefined,
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