0

List My Tasks

by
Published Oct 31, 2023

List the tasks in a user's My Tasks list in a workspace (`me` by default, other users only in organizations you share). 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
 * List My Tasks
28
 * List the tasks in a user's My Tasks list in a workspace (`me` by default, other users only in organizations you share). 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
  workspace: DynSelect_workspace,
33
  user: string | undefined,
34
  completed_since: string | undefined,
35
  opt_fields: string | undefined,
36
  limit: number | undefined,
37
  offset: string | undefined
38
) {
39
  const headers = {
40
    Authorization: `Bearer ${auth.token}`,
41
    Accept: "application/json",
42
  }
43

44
  const listUrl = new URL(
45
    `https://app.asana.com/api/1.0/users/${user || "me"}/user_task_list`
46
  )
47
  listUrl.searchParams.append("workspace", workspace)
48
  const listResponse = await fetch(listUrl, { headers })
49
  if (!listResponse.ok) {
50
    throw new Error(`${listResponse.status} ${await listResponse.text()}`)
51
  }
52
  const { data: taskList } = await listResponse.json()
53

54
  const url = new URL(
55
    `https://app.asana.com/api/1.0/user_task_lists/${taskList.gid}/tasks`
56
  )
57
  const params = { completed_since, opt_fields, limit: limit ?? 100, offset }
58
  for (const [key, value] of Object.entries(params)) {
59
    if (value != null && value !== "") {
60
      url.searchParams.append(key, String(value))
61
    }
62
  }
63

64
  const response = await fetch(url, { headers })
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 tasks from a user task list
    6
     * Returns the compact list of tasks in a user’s My Tasks list.
    7
     *Note: Access control is enforced for this endpoint as with all Asana API endpoints, meaning a user’s private tasks will be filtered out if the API-authenticated user does not have access to them.*
    8
     *Note: Both complete and incomplete tasks are returned by default unless they are filtered out (for example, setting `completed_since=now` will return only incomplete tasks, which is the default view for “My Tasks” in Asana.)*
    9
     */
    10
    export async function main(
    11
      auth: Asana,
    12
      user_task_list_gid: string,
    13
      completed_since: string | undefined,
    14
      opt_pretty: string | undefined,
    15
      opt_fields: string | undefined,
    16
      limit: string | undefined,
    17
      offset: string | undefined
    18
    ) {
    19
      const url = new URL(
    20
        `https://app.asana.com/api/1.0/user_task_lists/${user_task_list_gid}/tasks`
    21
      );
    22
      for (const [k, v] of [
    23
        ["completed_since", completed_since],
    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