0

Add Tag to Task

by
Published Oct 31, 2023

Add a tag to a task. The tag must be in the task's workspace.

Script asana Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Add Tag to Task
5
 * Add a tag to a task. The tag must be in the task's workspace.
6
 */
7
export async function main(auth: RT.Asana, task_gid: string, tag: string) {
8
  const response = await fetch(
9
    `https://app.asana.com/api/1.0/tasks/${task_gid}/addTag`,
10
    {
11
      method: "POST",
12
      headers: {
13
        Authorization: `Bearer ${auth.token}`,
14
        "Content-Type": "application/json",
15
        Accept: "application/json",
16
      },
17
      body: JSON.stringify({ data: { tag } }),
18
    }
19
  )
20

21
  if (!response.ok) {
22
    throw new Error(`${response.status} ${await response.text()}`)
23
  }
24

25
  return await response.json()
26
}
27

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Add a tag to a task
    6
     * Adds a tag to a task. Returns an empty data block.
    7
     */
    8
    export async function main(
    9
      auth: Asana,
    10
      task_gid: string,
    11
      opt_pretty: string | undefined,
    12
      opt_fields: string | undefined,
    13
      body: { data?: { tag: string; [k: string]: unknown }; [k: string]: unknown }
    14
    ) {
    15
      const url = new URL(`https://app.asana.com/api/1.0/tasks/${task_gid}/addTag`);
    16
      for (const [k, v] of [
    17
        ["opt_pretty", opt_pretty],
    18
        ["opt_fields", opt_fields],
    19
      ]) {
    20
        if (v !== undefined && v !== "") {
    21
          url.searchParams.append(k, v);
    22
        }
    23
      }
    24
      const response = await fetch(url, {
    25
        method: "POST",
    26
        headers: {
    27
          "Content-Type": "application/json",
    28
          Authorization: "Bearer " + auth.token,
    29
        },
    30
        body: JSON.stringify(body),
    31
      });
    32
      if (!response.ok) {
    33
        const text = await response.text();
    34
        throw new Error(`${response.status} ${text}`);
    35
      }
    36
      return await response.json();
    37
    }
    38