0

Create Comment

by
Published today

Add a comment to a page, or reply in an existing discussion thread.

Script notion Verified

The script

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

3
/**
4
 * Create Comment
5
 * Add a comment to a page, or reply in an existing discussion thread by passing `discussion_id` instead of `page_id`. Needs the insert comments capability.
6
 */
7
export async function main(
8
  auth: RT.Notion,
9
  text: string,
10
  page_id: string | undefined,
11
  discussion_id: string | undefined
12
) {
13
  const body: { [key: string]: any } = {
14
    rich_text: [{ text: { content: text } }],
15
  }
16
  if (discussion_id !== undefined && discussion_id !== "") {
17
    body.discussion_id = discussion_id
18
  } else {
19
    body.parent = { page_id }
20
  }
21

22
  const response = await fetch("https://api.notion.com/v1/comments", {
23
    method: "POST",
24
    headers: {
25
      Authorization: `Bearer ${auth.token}`,
26
      "Content-Type": "application/json",
27
      Accept: "application/json",
28
      "Notion-Version": "2026-03-11",
29
    },
30
    body: JSON.stringify(body),
31
  })
32

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

37
  return await response.json()
38
}
39