Edits history of script submission #23127 for ' Create Database (notion)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Database
     * Create a database under a page. `properties` maps column names to a Notion property schema, or to a type name for simple columns, e.g. {"Name": "title", "Points": "number", "Status": {"select": {"options": [{"name": "Todo"}]}}}. Exactly one column must be `title`. The response's `data_sources[0].id` is what Query Data Source and Create Page take.
     */
    export async function main(
      auth: RT.Notion,
      parent_page_id: string,
      title: string,
      properties: { [key: string]: any }
    ) {
      const schema: { [key: string]: any } = {}
      for (const [name, value] of Object.entries(properties)) {
        schema[name] = typeof value === "string" ? { [value]: {} } : value
      }
    
      const response = await fetch("https://api.notion.com/v1/databases", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
          "Notion-Version": "2026-03-11",
        },
        body: JSON.stringify({
          parent: { type: "page_id", page_id: parent_page_id },
          title: [{ text: { content: title } }],
          initial_data_source: { properties: schema },
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago