0

List Workspaces

by
Published Oct 31, 2023

List the workspaces and organizations the token can access. Most other calls need one of these gids. 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
/**
4
 * List Workspaces
5
 * List the workspaces and organizations the token can access. Most other calls need one of these gids. Pass `next_page.offset` from a response as `offset` to get the next page.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  opt_fields: string | undefined,
10
  limit: number | undefined,
11
  offset: string | undefined
12
) {
13
  const url = new URL("https://app.asana.com/api/1.0/workspaces")
14
  const params = { opt_fields, limit: limit ?? 100, offset }
15
  for (const [key, value] of Object.entries(params)) {
16
    if (value != null && value !== "") {
17
      url.searchParams.append(key, String(value))
18
    }
19
  }
20

21
  const response = await fetch(url, {
22
    headers: {
23
      Authorization: `Bearer ${auth.token}`,
24
      Accept: "application/json",
25
    },
26
  })
27

28
  if (!response.ok) {
29
    throw new Error(`${response.status} ${await response.text()}`)
30
  }
31

32
  return await response.json()
33
}
34

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get multiple workspaces
    6
     * Returns the compact records for all workspaces visible to the authorized user.
    7
     */
    8
    export async function main(
    9
      auth: Asana,
    10
      opt_pretty: string | undefined,
    11
      opt_fields: string | undefined,
    12
      limit: string | undefined,
    13
      offset: string | undefined
    14
    ) {
    15
      const url = new URL(`https://app.asana.com/api/1.0/workspaces`);
    16
      for (const [k, v] of [
    17
        ["opt_pretty", opt_pretty],
    18
        ["opt_fields", opt_fields],
    19
        ["limit", limit],
    20
        ["offset", offset],
    21
      ]) {
    22
        if (v !== undefined && v !== "") {
    23
          url.searchParams.append(k, v);
    24
        }
    25
      }
    26
      const response = await fetch(url, {
    27
        method: "GET",
    28
        headers: {
    29
          Authorization: "Bearer " + auth.token,
    30
        },
    31
        body: undefined,
    32
      });
    33
      if (!response.ok) {
    34
        const text = await response.text();
    35
        throw new Error(`${response.status} ${text}`);
    36
      }
    37
      return await response.json();
    38
    }
    39