Get all projects in a workspace

Returns the compact project records for all projects in the workspace. *Note: This endpoint may timeout for large domains. Prefer the `/teams/{team_gid}/projects` endpoint.*

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 all projects in a workspace
6
 * Returns the compact project records for all projects in the workspace.
7
 *Note: This endpoint may timeout for large domains. Prefer the `/teams/{team_gid}/projects` endpoint.*
8
 */
9
export async function main(
10
  auth: Asana,
11
  workspace_gid: string,
12
  opt_pretty: string | undefined,
13
  opt_fields: string | undefined,
14
  limit: string | undefined,
15
  offset: string | undefined,
16
  archived: string | undefined
17
) {
18
  const url = new URL(
19
    `https://app.asana.com/api/1.0/workspaces/${workspace_gid}/projects`
20
  );
21
  for (const [k, v] of [
22
    ["opt_pretty", opt_pretty],
23
    ["opt_fields", opt_fields],
24
    ["limit", limit],
25
    ["offset", offset],
26
    ["archived", archived],
27
  ]) {
28
    if (v !== undefined && v !== "") {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45