0

List Projects

by
Published Oct 31, 2023

List the projects in a workspace, optionally only those 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 Projects
28
 * List the projects in a workspace, optionally only those 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
  archived: "any" | "active" | "archived" | undefined,
35
  opt_fields: string | undefined,
36
  limit: number | undefined,
37
  offset: string | undefined
38
) {
39
  const url = new URL(`https://app.asana.com/api/1.0/projects`)
40
  const params = {
41
    workspace,
42
    team,
43
    opt_fields,
44
    archived:
45
      archived === "active"
46
        ? false
47
        : archived === "archived"
48
          ? true
49
          : undefined,
50
    limit: limit ?? 100,
51
    offset,
52
  }
53
  for (const [key, value] of Object.entries(params)) {
54
    if (value != null && value !== "") {
55
      url.searchParams.append(key, String(value))
56
    }
57
  }
58

59
  const response = await fetch(url, {
60
    headers: {
61
      Authorization: `Bearer ${auth.token}`,
62
      Accept: "application/json",
63
    },
64
  })
65

66
  if (!response.ok) {
67
    throw new Error(`${response.status} ${await response.text()}`)
68
  }
69

70
  return await response.json()
71
}
72

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get multiple projects
    6
     * Returns the compact project records for some filtered set of projects. Use one or more of the parameters provided to filter the projects returned.
    7
     *Note: This endpoint may timeout for large domains. Try filtering by team!*
    8
     */
    9
    export async function main(
    10
      auth: Asana,
    11
      opt_pretty: string | undefined,
    12
      opt_fields: string | undefined,
    13
      limit: string | undefined,
    14
      offset: string | undefined,
    15
      workspace: string | undefined,
    16
      team: string | undefined,
    17
      archived: string | undefined
    18
    ) {
    19
      const url = new URL(`https://app.asana.com/api/1.0/projects`);
    20
      for (const [k, v] of [
    21
        ["opt_pretty", opt_pretty],
    22
        ["opt_fields", opt_fields],
    23
        ["limit", limit],
    24
        ["offset", offset],
    25
        ["workspace", workspace],
    26
        ["team", team],
    27
        ["archived", archived],
    28
      ]) {
    29
        if (v !== undefined && v !== "") {
    30
          url.searchParams.append(k, v);
    31
        }
    32
      }
    33
      const response = await fetch(url, {
    34
        method: "GET",
    35
        headers: {
    36
          Authorization: "Bearer " + auth.token,
    37
        },
    38
        body: undefined,
    39
      });
    40
      if (!response.ok) {
    41
        const text = await response.text();
    42
        throw new Error(`${response.status} ${text}`);
    43
      }
    44
      return await response.json();
    45
    }
    46