0

Create comment

by
Published Oct 17, 2025

Adds a comment by the user to a specific file, or as a reply to an other comment.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Create comment
7
 * Adds a comment by the user to a specific file, or
8
as a reply to an other comment.
9
 */
10
export async function main(
11
  auth: Box,
12
  fields: string | undefined,
13
  body: {
14
    message: string;
15
    tagged_message?: string;
16
    item: { id: string; type: "file" | "comment" };
17
  },
18
) {
19
  const url = new URL(`https://api.box.com/2.0/comments`);
20
  for (const [k, v] of [["fields", fields]]) {
21
    if (v !== undefined && v !== "" && k !== undefined) {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39