Get task count of a project

Get an object that holds task count fields. **All fields are excluded by default**. You must opt in using `opt_fields` to get any information from this endpoint. This endpoint has an additional rate limit and each field counts especially high against our cost limits. Milestones are just tasks, so they are included in the `num_tasks`, `num_incomplete_tasks`, and `num_completed_tasks` counts.

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 task count of a project
6
 * Get an object that holds task count fields. **All fields are excluded by default**. You must opt in using `opt_fields` to get any information from this endpoint.
7

8
This endpoint has an additional rate limit and each field counts especially high against our cost limits.
9

10
Milestones are just tasks, so they are included in the `num_tasks`, `num_incomplete_tasks`, and `num_completed_tasks` counts.
11
 */
12
export async function main(
13
  auth: Asana,
14
  project_gid: string,
15
  opt_pretty: string | undefined,
16
  opt_fields: string | undefined,
17
  limit: string | undefined,
18
  offset: string | undefined
19
) {
20
  const url = new URL(
21
    `https://app.asana.com/api/1.0/projects/${project_gid}/task_counts`
22
  );
23
  for (const [k, v] of [
24
    ["opt_pretty", opt_pretty],
25
    ["opt_fields", opt_fields],
26
    ["limit", limit],
27
    ["offset", offset],
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