0

Add Project to Task

by
Published Oct 31, 2023

Add a task to a project, optionally in a given section or next to another task. Calling it again for the same project moves the task.

Script asana Verified

The script

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

3
/**
4
 * Add Project to Task
5
 * Add a task to a project, optionally in a given section or next to another task. Calling it again for the same project moves the task.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  task_gid: string,
10
  project: string,
11
  section: string | undefined,
12
  insert_before: string | undefined,
13
  insert_after: string | undefined
14
) {
15
  const response = await fetch(
16
    `https://app.asana.com/api/1.0/tasks/${task_gid}/addProject`,
17
    {
18
      method: "POST",
19
      headers: {
20
        Authorization: `Bearer ${auth.token}`,
21
        "Content-Type": "application/json",
22
        Accept: "application/json",
23
      },
24
      body: JSON.stringify({
25
        data: {
26
          project,
27
          section: section || undefined,
28
          insert_before: insert_before || undefined,
29
          insert_after: insert_after || undefined,
30
        },
31
      }),
32
    }
33
  )
34

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  return await response.json()
40
}
41

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