Create a story on a task

Adds a story to a task. This endpoint currently only allows for comment stories to be created. The comment will be authored by the currently authenticated user, and timestamped when the server receives the request. Returns the full record for the new story added to the task.

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 story on a task
6
 * Adds a story to a task. This endpoint currently only allows for comment
7
stories to be created. The comment will be authored by the currently
8
authenticated user, and timestamped when the server receives the request.
9

10
Returns the full record for the new story added to the task.
11
 */
12
export async function main(
13
  auth: Asana,
14
  task_gid: string,
15
  opt_pretty: string | undefined,
16
  opt_fields: string | undefined,
17
  body: {
18
    data?: { gid?: string; resource_type?: string; [k: string]: unknown } & {
19
      created_at?: string;
20
      html_text?: string;
21
      is_pinned?: boolean;
22
      resource_subtype?: string;
23
      sticker_name?:
24
        | "green_checkmark"
25
        | "people_dancing"
26
        | "dancing_unicorn"
27
        | "heart"
28
        | "party_popper"
29
        | "people_waving_flags"
30
        | "splashing_narwhal"
31
        | "trophy"
32
        | "yeti_riding_unicorn"
33
        | "celebrating_people"
34
        | "determined_climbers"
35
        | "phoenix_spreading_love";
36
      text?: string;
37
      [k: string]: unknown;
38
    };
39
    [k: string]: unknown;
40
  }
41
) {
42
  const url = new URL(
43
    `https://app.asana.com/api/1.0/tasks/${task_gid}/stories`
44
  );
45
  for (const [k, v] of [
46
    ["opt_pretty", opt_pretty],
47
    ["opt_fields", opt_fields],
48
  ]) {
49
    if (v !== undefined && v !== "") {
50
      url.searchParams.append(k, v);
51
    }
52
  }
53
  const response = await fetch(url, {
54
    method: "POST",
55
    headers: {
56
      "Content-Type": "application/json",
57
      Authorization: "Bearer " + auth.token,
58
    },
59
    body: JSON.stringify(body),
60
  });
61
  if (!response.ok) {
62
    const text = await response.text();
63
    throw new Error(`${response.status} ${text}`);
64
  }
65
  return await response.json();
66
}
67