0

Find Tasks

by
Published Oct 31, 2023

List the tasks of a project, of a section, or assigned to a user in a workspace (set exactly one of project, section or assignee; assignee also needs workspace). Works on every Asana plan. Set `completed_since` to `now` to get only incomplete tasks. 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
 * Find Tasks
28
 * List the tasks of a project, of a section, or assigned to a user in a workspace (set exactly one of project, section or assignee; assignee also needs workspace). Works on every Asana plan. Set `completed_since` to `now` to get only incomplete tasks. 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
  project: string | undefined,
33
  section: string | undefined,
34
  assignee: string | undefined,
35
  workspace: DynSelect_workspace | undefined,
36
  completed_since: string | undefined,
37
  modified_since: string | undefined,
38
  opt_fields: string | undefined,
39
  limit: number | undefined,
40
  offset: string | undefined
41
) {
42
  const url = new URL("https://app.asana.com/api/1.0/tasks")
43
  const params = {
44
    project,
45
    section,
46
    assignee,
47
    workspace: assignee ? workspace : undefined,
48
    completed_since,
49
    modified_since,
50
    opt_fields,
51
    limit: limit ?? 100,
52
    offset,
53
  }
54
  for (const [key, value] of Object.entries(params)) {
55
    if (value != null && value !== "") {
56
      url.searchParams.append(key, String(value))
57
    }
58
  }
59

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

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

71
  return await response.json()
72
}
73

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get multiple tasks
    6
     * Returns the compact task records for some filtered set of tasks. Use one or more of the parameters provided to filter the tasks returned. You must specify a `project` or `tag` if you do not specify `assignee` and `workspace`.
    7
    
    
    8
    For more complex task retrieval, use workspaces/{workspace_gid}/tasks/search.
    9
     */
    10
    export async function main(
    11
      auth: Asana,
    12
      opt_pretty: string | undefined,
    13
      opt_fields: string | undefined,
    14
      limit: string | undefined,
    15
      offset: string | undefined,
    16
      assignee: string | undefined,
    17
      project: string | undefined,
    18
      section: string | undefined,
    19
      workspace: string | undefined,
    20
      completed_since: string | undefined,
    21
      modified_since: string | undefined
    22
    ) {
    23
      const url = new URL(`https://app.asana.com/api/1.0/tasks`);
    24
      for (const [k, v] of [
    25
        ["opt_pretty", opt_pretty],
    26
        ["opt_fields", opt_fields],
    27
        ["limit", limit],
    28
        ["offset", offset],
    29
        ["assignee", assignee],
    30
        ["project", project],
    31
        ["section", section],
    32
        ["workspace", workspace],
    33
        ["completed_since", completed_since],
    34
        ["modified_since", modified_since],
    35
      ]) {
    36
        if (v !== undefined && v !== "") {
    37
          url.searchParams.append(k, v);
    38
        }
    39
      }
    40
      const response = await fetch(url, {
    41
        method: "GET",
    42
        headers: {
    43
          Authorization: "Bearer " + auth.token,
    44
        },
    45
        body: undefined,
    46
      });
    47
      if (!response.ok) {
    48
        const text = await response.text();
    49
        throw new Error(`${response.status} ${text}`);
    50
      }
    51
      return await response.json();
    52
    }
    53