0

Create Section

by
Published Oct 31, 2023

Create a section (a board column, or a heading in list view) in a project. Without insert_before/insert_after it is added at the end.

Script asana Verified

The script

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

3
/**
4
 * Create Section
5
 * Create a section (a board column, or a heading in list view) in a project. Without insert_before/insert_after it is added at the end.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  project_gid: string,
10
  name: string,
11
  insert_before: string | undefined,
12
  insert_after: string | undefined
13
) {
14
  const response = await fetch(
15
    `https://app.asana.com/api/1.0/projects/${project_gid}/sections`,
16
    {
17
      method: "POST",
18
      headers: {
19
        Authorization: `Bearer ${auth.token}`,
20
        "Content-Type": "application/json",
21
        Accept: "application/json",
22
      },
23
      body: JSON.stringify({
24
        data: {
25
          name,
26
          insert_before: insert_before || undefined,
27
          insert_after: insert_after || undefined,
28
        },
29
      }),
30
    }
31
  )
32

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

37
  return await response.json()
38
}
39

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Create a section in a project
    6
     * Creates a new section in a project.
    7
    Returns the full record of the newly created section.
    8
     */
    9
    export async function main(
    10
      auth: Asana,
    11
      project_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
          name: string;
    19
          [k: string]: unknown;
    20
        };
    21
        [k: string]: unknown;
    22
      }
    23
    ) {
    24
      const url = new URL(
    25
        `https://app.asana.com/api/1.0/projects/${project_gid}/sections`
    26
      );
    27
      for (const [k, v] of [
    28
        ["opt_pretty", opt_pretty],
    29
        ["opt_fields", opt_fields],
    30
      ]) {
    31
        if (v !== undefined && v !== "") {
    32
          url.searchParams.append(k, v);
    33
        }
    34
      }
    35
      const response = await fetch(url, {
    36
        method: "POST",
    37
        headers: {
    38
          "Content-Type": "application/json",
    39
          Authorization: "Bearer " + auth.token,
    40
        },
    41
        body: JSON.stringify(body),
    42
      });
    43
      if (!response.ok) {
    44
        const text = await response.text();
    45
        throw new Error(`${response.status} ${text}`);
    46
      }
    47
      return await response.json();
    48
    }
    49