0
Create a tag
One script reply has been approved by the moderators Verified

Creates a new tag in a workspace or organization.

Every tag is required to be created in a specific workspace or organization, and this cannot be changed once set. Note that you can use the workspace parameter regardless of whether or not it is an organization.

Returns the full record of the newly created tag.

Created by hugo697 313 days ago Viewed 8967 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 313 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Create a tag
6
 * Creates a new tag in a workspace or organization.
7

8
Every tag is required to be created in a specific workspace or
9
organization, and this cannot be changed once set. Note that you can use
10
the workspace parameter regardless of whether or not it is an
11
organization.
12

13
Returns the full record of the newly created tag.
14
 */
15
export async function main(
16
  auth: Asana,
17
  opt_pretty: string | undefined,
18
  opt_fields: string | undefined,
19
  body: {
20
    data?: (({ gid?: string; resource_type?: string; [k: string]: unknown } & {
21
      name?: string;
22
      [k: string]: unknown;
23
    }) & {
24
      color?:
25
        | "dark-pink"
26
        | "dark-green"
27
        | "dark-blue"
28
        | "dark-red"
29
        | "dark-teal"
30
        | "dark-brown"
31
        | "dark-orange"
32
        | "dark-purple"
33
        | "dark-warm-gray"
34
        | "light-pink"
35
        | "light-green"
36
        | "light-blue"
37
        | "light-red"
38
        | "light-teal"
39
        | "light-brown"
40
        | "light-orange"
41
        | "light-purple"
42
        | "light-warm-gray";
43
      notes?: string;
44
      [k: string]: unknown;
45
    }) & { followers?: string[]; workspace?: string; [k: string]: unknown };
46
    [k: string]: unknown;
47
  }
48
) {
49
  const url = new URL(`https://app.asana.com/api/1.0/tags`);
50
  for (const [k, v] of [
51
    ["opt_pretty", opt_pretty],
52
    ["opt_fields", opt_fields],
53
  ]) {
54
    if (v !== undefined && v !== "") {
55
      url.searchParams.append(k, v);
56
    }
57
  }
58
  const response = await fetch(url, {
59
    method: "POST",
60
    headers: {
61
      "Content-Type": "application/json",
62
      Authorization: "Bearer " + auth.token,
63
    },
64
    body: JSON.stringify(body),
65
  });
66
  if (!response.ok) {
67
    const text = await response.text();
68
    throw new Error(`${response.status} ${text}`);
69
  }
70
  return await response.json();
71
}
72