0

List Users

by
Published Oct 31, 2023

List the users in a workspace, optionally only the members of one team. Pass `next_page.offset` from a response as `offset` to get the next page.

Script asana Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
export type DynSelect_workspace = string
4

5
// Resolver: the workspaces and organizations the token can access.
6
export async function workspace(auth: RT.Asana) {
7
  const response = await fetch(
8
    "https://app.asana.com/api/1.0/workspaces?limit=100",
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { data } = await response.json()
20
  return data.map((w: { gid: string; name: string }) => ({
21
    value: w.gid,
22
    label: w.name,
23
  }))
24
}
25

26
/**
27
 * List Users
28
 * List the users in a workspace, optionally only the members of one team. Pass `next_page.offset` from a response as `offset` to get the next page.
29
 */
30
export async function main(
31
  auth: RT.Asana,
32
  workspace: DynSelect_workspace,
33
  team: string | undefined,
34
  opt_fields: string | undefined,
35
  limit: number | undefined,
36
  offset: string | undefined
37
) {
38
  const url = new URL(`https://app.asana.com/api/1.0/users`)
39
  const params = { workspace, team, opt_fields, limit: limit ?? 100, offset }
40
  for (const [key, value] of Object.entries(params)) {
41
    if (value != null && value !== "") {
42
      url.searchParams.append(key, String(value))
43
    }
44
  }
45

46
  const response = await fetch(url, {
47
    headers: {
48
      Authorization: `Bearer ${auth.token}`,
49
      Accept: "application/json",
50
    },
51
  })
52

53
  if (!response.ok) {
54
    throw new Error(`${response.status} ${await response.text()}`)
55
  }
56

57
  return await response.json()
58
}
59

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 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