Edits history of script submission #23128 for ' Create Page (notion)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Page
     * 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.
     */
    export async function main(
      auth: RT.Notion,
      parent_id: string,
      parent_type: "page_id" | "data_source_id" = "page_id",
      title: string | undefined,
      markdown: string | undefined,
      properties: { [key: string]: any } | undefined,
      icon_emoji: string | undefined
    ) {
      const body: { [key: string]: any } = {
        parent: { type: parent_type, [parent_type]: parent_id },
        properties: { ...properties },
      }
      if (title !== undefined && title !== "") {
        body.properties.title = { title: [{ text: { content: title } }] }
      }
      // An empty `properties` stops Notion from taking the title from the first "# h1"
      if (!Object.keys(body.properties).length) delete body.properties
      if (markdown !== undefined && markdown !== "") body.markdown = markdown
      if (icon_emoji !== undefined && icon_emoji !== "") {
        body.icon = { type: "emoji", emoji: icon_emoji }
      }
    
      const response = await fetch("https://api.notion.com/v1/pages", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
          "Notion-Version": "2026-03-11",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago