Create Task Attachment
One script reply has been approved by the moderators Verified

Upload a file to a task as an attachment. Files stored in the cloud cannot be used in this API request.

Note: This request uses multipart/form-data as the content type.

Created by hugo697 168 days ago
Submitted by hugo697 Bun
Verified 168 days ago
1
//native
2
type Clickup = {
3
  token: string;
4
};
5
/**
6
 * Create Task Attachment
7
 * Upload a file to a task as an attachment. Files stored in the cloud cannot be used in this API request.\
8
 \
9
***Note:** This request uses multipart/form-data as the content type.*
10
 */
11
export async function main(
12
  auth: Clickup,
13
  task_id: string,
14
  custom_task_ids: string | undefined,
15
  team_id: string | undefined,
16
  body: { attachment?: unknown[] },
17
) {
18
  const url = new URL(
19
    `https://api.clickup.com/api/v2/task/${task_id}/attachment`,
20
  );
21
  for (const [k, v] of [
22
    ["custom_task_ids", custom_task_ids],
23
    ["team_id", team_id],
24
  ]) {
25
    if (v !== undefined && v !== "" && k !== undefined) {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const formData = new FormData();
30
  for (const [k, v] of Object.entries(body)) {
31
    if (v !== undefined) {
32
      formData.append(k, String(v));
33
    }
34
  }
35
  const response = await fetch(url, {
36
    method: "POST",
37
    headers: {
38
      Authorization: auth.token,
39
    },
40
    body: formData,
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48