1 | |
2 |
|
3 | |
4 | * Create Database |
5 | * 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. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Notion, |
9 | parent_page_id: string, |
10 | title: string, |
11 | properties: { [key: string]: any } |
12 | ) { |
13 | const schema: { [key: string]: any } = {} |
14 | for (const [name, value] of Object.entries(properties)) { |
15 | schema[name] = typeof value === "string" ? { [value]: {} } : value |
16 | } |
17 |
|
18 | const response = await fetch("https://api.notion.com/v1/databases", { |
19 | method: "POST", |
20 | headers: { |
21 | Authorization: `Bearer ${auth.token}`, |
22 | "Content-Type": "application/json", |
23 | Accept: "application/json", |
24 | "Notion-Version": "2026-03-11", |
25 | }, |
26 | body: JSON.stringify({ |
27 | parent: { type: "page_id", page_id: parent_page_id }, |
28 | title: [{ text: { content: title } }], |
29 | initial_data_source: { properties: schema }, |
30 | }), |
31 | }) |
32 |
|
33 | if (!response.ok) { |
34 | throw new Error(`${response.status} ${await response.text()}`) |
35 | } |
36 |
|
37 | return await response.json() |
38 | } |
39 |
|