0

Retrieve Data Source

by
Published today

Get a data source's schema: its property names, types and select options.

Script notion Verified

The script

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

3
export type DynSelect_data_source_id = string
4

5
export async function data_source_id(auth: RT.Notion) {
6
  const response = await fetch("https://api.notion.com/v1/search", {
7
    method: "POST",
8
    headers: {
9
      Authorization: `Bearer ${auth.token}`,
10
      "Content-Type": "application/json",
11
      Accept: "application/json",
12
      "Notion-Version": "2026-03-11",
13
    },
14
    body: JSON.stringify({
15
      filter: { property: "object", value: "data_source" },
16
    }),
17
  })
18
  if (!response.ok) {
19
    throw new Error(`${response.status} ${await response.text()}`)
20
  }
21
  const { results } = (await response.json()) as {
22
    results: { id: string; title: { plain_text: string }[] }[]
23
  }
24
  return results.map((d) => ({
25
    value: d.id,
26
    label: d.title.map((t) => t.plain_text).join("") || d.id,
27
  }))
28
}
29

30
/**
31
 * Retrieve Data Source
32
 * Get a data source's schema: its property names, types and select/status options. Read it before building a Query Data Source filter or Create Page properties.
33
 */
34
export async function main(
35
  auth: RT.Notion,
36
  data_source_id: DynSelect_data_source_id
37
) {
38
  const response = await fetch(
39
    `https://api.notion.com/v1/data_sources/${data_source_id}`,
40
    {
41
      headers: {
42
        Authorization: `Bearer ${auth.token}`,
43
        Accept: "application/json",
44
        "Notion-Version": "2026-03-11",
45
      },
46
    }
47
  )
48

49
  if (!response.ok) {
50
    throw new Error(`${response.status} ${await response.text()}`)
51
  }
52

53
  return await response.json()
54
}
55