0

Add a comment to a file

by
Published Apr 8, 2025

Posts a new comment on the file.

Script figma Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Figma = {
3
  token: string;
4
};
5
/**
6
 * Add a comment to a file
7
 * Posts a new comment on the file.
8
 */
9
export async function main(
10
  auth: Figma,
11
  file_key: string,
12
  body: {
13
    message: string;
14
    comment_id?: string;
15
    client_meta?:
16
      | { x: number; y: number }
17
      | { node_id: string; node_offset: { x: number; y: number } }
18
      | {
19
          x: number;
20
          y: number;
21
          region_height: number;
22
          region_width: number;
23
          comment_pin_corner?:
24
            | "top-left"
25
            | "top-right"
26
            | "bottom-left"
27
            | "bottom-right";
28
        }
29
      | {
30
          node_id: string;
31
          node_offset: { x: number; y: number };
32
          region_height: number;
33
          region_width: number;
34
          comment_pin_corner?:
35
            | "top-left"
36
            | "top-right"
37
            | "bottom-left"
38
            | "bottom-right";
39
        };
40
  },
41
) {
42
  const url = new URL(`https://api.figma.com/v1/files/${file_key}/comments`);
43

44
  const response = await fetch(url, {
45
    method: "POST",
46
    headers: {
47
      "Content-Type": "application/json",
48
      Authorization: "Bearer " + auth.token,
49
    },
50
    body: JSON.stringify(body),
51
  });
52
  if (!response.ok) {
53
    const text = await response.text();
54
    throw new Error(`${response.status} ${text}`);
55
  }
56
  return await response.json();
57
}
58