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

Creating a new task is as easy as POSTing to the /tasks endpoint with a data block containing the fields you’d like to set on the task. Any unspecified fields will take on default values.

Every task is required to be created in a specific workspace, and this workspace cannot be changed once set. The workspace need not be set explicitly if you specify projects or a parent task instead.

Created by hugo697 418 days ago Viewed 9248 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 418 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Create a task
6
 * Creating a new task is as easy as POSTing to the `/tasks` endpoint with a
7
data block containing the fields you’d like to set on the task. Any
8
unspecified fields will take on default values.
9

10
Every task is required to be created in a specific workspace, and this
11
workspace cannot be changed once set. The workspace need not be set
12
explicitly if you specify `projects` or a `parent` task instead.
13
 */
14
export async function main(
15
  auth: Asana,
16
  opt_pretty: string | undefined,
17
  opt_fields: string | undefined,
18
  body: {
19
    data?: (({ gid?: string; resource_type?: string; [k: string]: unknown } & {
20
      name?: string;
21
      resource_subtype?: "default_task" | "milestone" | "section" | "approval";
22
      [k: string]: unknown;
23
    }) & {
24
      actual_time_minutes?: number;
25
      approval_status?:
26
        | "pending"
27
        | "approved"
28
        | "rejected"
29
        | "changes_requested";
30
      assignee_status?: "today" | "upcoming" | "later" | "new" | "inbox";
31
      completed?: boolean;
32
      completed_at?: string;
33
      completed_by?: {
34
        gid?: string;
35
        resource_type?: string;
36
        [k: string]: unknown;
37
      } & { name?: string; [k: string]: unknown };
38
      created_at?: string;
39
      dependencies?: {
40
        gid?: string;
41
        resource_type?: string;
42
        [k: string]: unknown;
43
      }[];
44
      dependents?: {
45
        gid?: string;
46
        resource_type?: string;
47
        [k: string]: unknown;
48
      }[];
49
      due_at?: string;
50
      due_on?: string;
51
      external?: { data?: string; gid?: string; [k: string]: unknown };
52
      hearted?: boolean;
53
      hearts?: {
54
        gid?: string;
55
        user?: {
56
          gid?: string;
57
          resource_type?: string;
58
          [k: string]: unknown;
59
        } & { name?: string; [k: string]: unknown };
60
        [k: string]: unknown;
61
      }[];
62
      html_notes?: string;
63
      is_rendered_as_separator?: boolean;
64
      liked?: boolean;
65
      likes?: {
66
        gid?: string;
67
        user?: {
68
          gid?: string;
69
          resource_type?: string;
70
          [k: string]: unknown;
71
        } & { name?: string; [k: string]: unknown };
72
        [k: string]: unknown;
73
      }[];
74
      memberships?: {
75
        project?: {
76
          gid?: string;
77
          resource_type?: string;
78
          [k: string]: unknown;
79
        } & { name?: string; [k: string]: unknown };
80
        section?: {
81
          gid?: string;
82
          resource_type?: string;
83
          [k: string]: unknown;
84
        } & { name?: string; [k: string]: unknown };
85
        [k: string]: unknown;
86
      }[];
87
      modified_at?: string;
88
      name?: string;
89
      notes?: string;
90
      num_hearts?: number;
91
      num_likes?: number;
92
      num_subtasks?: number;
93
      start_at?: string;
94
      start_on?: string;
95
      [k: string]: unknown;
96
    }) & {
97
      assignee?: string;
98
      assignee_section?: string;
99
      custom_fields?: { [k: string]: string };
100
      followers?: string[];
101
      parent?: string;
102
      projects?: string[];
103
      tags?: string[];
104
      workspace?: string;
105
      [k: string]: unknown;
106
    };
107
    [k: string]: unknown;
108
  }
109
) {
110
  const url = new URL(`https://app.asana.com/api/1.0/tasks`);
111
  for (const [k, v] of [
112
    ["opt_pretty", opt_pretty],
113
    ["opt_fields", opt_fields],
114
  ]) {
115
    if (v !== undefined && v !== "") {
116
      url.searchParams.append(k, v);
117
    }
118
  }
119
  const response = await fetch(url, {
120
    method: "POST",
121
    headers: {
122
      "Content-Type": "application/json",
123
      Authorization: "Bearer " + auth.token,
124
    },
125
    body: JSON.stringify(body),
126
  });
127
  if (!response.ok) {
128
    const text = await response.text();
129
    throw new Error(`${response.status} ${text}`);
130
  }
131
  return await response.json();
132
}
133