0

Create Page

by
Published today

Create a page under a parent page, or a row in a database's data source, with optional Markdown content.

Script notion Verified

The script

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

3
/**
4
 * Create Page
5
 * Create a page under a parent page, or a row in a database's data source. `markdown` is the page body as Notion-flavored Markdown; `properties` takes Notion property values keyed by name for data source rows. Needs the insert content capability.
6
 */
7
export async function main(
8
  auth: RT.Notion,
9
  parent_id: string,
10
  parent_type: "page_id" | "data_source_id" = "page_id",
11
  title: string | undefined,
12
  markdown: string | undefined,
13
  properties: { [key: string]: any } | undefined,
14
  icon_emoji: string | undefined
15
) {
16
  const body: { [key: string]: any } = {
17
    parent: { type: parent_type, [parent_type]: parent_id },
18
    properties: { ...properties },
19
  }
20
  if (title !== undefined && title !== "") {
21
    body.properties.title = { title: [{ text: { content: title } }] }
22
  }
23
  // An empty `properties` stops Notion from taking the title from the first "# h1"
24
  if (!Object.keys(body.properties).length) delete body.properties
25
  if (markdown !== undefined && markdown !== "") body.markdown = markdown
26
  if (icon_emoji !== undefined && icon_emoji !== "") {
27
    body.icon = { type: "emoji", emoji: icon_emoji }
28
  }
29

30
  const response = await fetch("https://api.notion.com/v1/pages", {
31
    method: "POST",
32
    headers: {
33
      Authorization: `Bearer ${auth.token}`,
34
      "Content-Type": "application/json",
35
      Accept: "application/json",
36
      "Notion-Version": "2026-03-11",
37
    },
38
    body: JSON.stringify(body),
39
  })
40

41
  if (!response.ok) {
42
    throw new Error(`${response.status} ${await response.text()}`)
43
  }
44

45
  return await response.json()
46
}
47