Create a tag in a workspace

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.

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
 * Create a tag in a workspace
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
  workspace_gid: string,
18
  opt_pretty: string | undefined,
19
  opt_fields: string | undefined,
20
  body: {
21
    data?: (({ gid?: string; resource_type?: string; [k: string]: unknown } & {
22
      name?: string;
23
      [k: string]: unknown;
24
    }) & {
25
      color?:
26
        | "dark-pink"
27
        | "dark-green"
28
        | "dark-blue"
29
        | "dark-red"
30
        | "dark-teal"
31
        | "dark-brown"
32
        | "dark-orange"
33
        | "dark-purple"
34
        | "dark-warm-gray"
35
        | "light-pink"
36
        | "light-green"
37
        | "light-blue"
38
        | "light-red"
39
        | "light-teal"
40
        | "light-brown"
41
        | "light-orange"
42
        | "light-purple"
43
        | "light-warm-gray";
44
      notes?: string;
45
      [k: string]: unknown;
46
    }) & {
47
      created_at?: string;
48
      followers?: ({
49
        gid?: string;
50
        resource_type?: string;
51
        [k: string]: unknown;
52
      } & { name?: string; [k: string]: unknown })[];
53
      permalink_url?: string;
54
      workspace?: {
55
        gid?: string;
56
        resource_type?: string;
57
        [k: string]: unknown;
58
      } & { name?: string; [k: string]: unknown };
59
      [k: string]: unknown;
60
    };
61
    [k: string]: unknown;
62
  }
63
) {
64
  const url = new URL(
65
    `https://app.asana.com/api/1.0/workspaces/${workspace_gid}/tags`
66
  );
67
  for (const [k, v] of [
68
    ["opt_pretty", opt_pretty],
69
    ["opt_fields", opt_fields],
70
  ]) {
71
    if (v !== undefined && v !== "") {
72
      url.searchParams.append(k, v);
73
    }
74
  }
75
  const response = await fetch(url, {
76
    method: "POST",
77
    headers: {
78
      "Content-Type": "application/json",
79
      Authorization: "Bearer " + auth.token,
80
    },
81
    body: JSON.stringify(body),
82
  });
83
  if (!response.ok) {
84
    const text = await response.text();
85
    throw new Error(`${response.status} ${text}`);
86
  }
87
  return await response.json();
88
}
89