0

Retrieve Block Children

by
Published Apr 25, 2023

List the direct child blocks of a page or block.

Script notion Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
/**
4
 * Retrieve Block Children
5
 * List the direct child blocks of a page or block (one level; recurse on blocks with `has_children`). Paginate with `next_cursor` as `start_cursor`.
6
 */
7
export async function main(
8
  auth: RT.Notion,
9
  block_id: string,
10
  start_cursor: string | undefined,
11
  page_size: number | undefined
12
) {
13
  const url = new URL(`https://api.notion.com/v1/blocks/${block_id}/children`)
14
  if (start_cursor !== undefined && start_cursor !== "") {
15
    url.searchParams.append("start_cursor", start_cursor)
16
  }
17
  if (page_size !== undefined) {
18
    url.searchParams.append("page_size", String(page_size))
19
  }
20

21
  const response = await fetch(url, {
22
    headers: {
23
      Authorization: `Bearer ${auth.token}`,
24
      Accept: "application/json",
25
      "Notion-Version": "2026-03-11",
26
    },
27
  })
28

29
  if (!response.ok) {
30
    throw new Error(`${response.status} ${await response.text()}`)
31
  }
32

33
  return await response.json()
34
}
35

Other submissions
  • Submitted by adam186 Deno
    Created 519 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { Client } from "npm:@notionhq/client";
    3
    
    
    4
    /**
    5
     * Learn more at
    6
     * https://developers.notion.com/reference/get-block-children
    7
     */
    8
    type Notion = {
    9
      token: string;
    10
    };
    11
    export async function main(
    12
      auth: Notion,
    13
      block_id: string,
    14
      start_cursor?: string,
    15
      page_size?: number,
    16
    ) {
    17
      const client = new Client({ auth: auth.token });
    18
      const args = removeObjectEmptyFields({
    19
        block_id,
    20
        start_cursor,
    21
        page_size,
    22
      });
    23
      return await client.blocks.children.list(args);
    24
    }
    25
    
    
  • Submitted by hugo989 Bun
    Created 127 days ago
    1
    import { Client } from "@notionhq/client";
    2
    
    
    3
    /**
    4
     * Learn more at
    5
     * https://developers.notion.com/reference/get-block-children
    6
     */
    7
    type Notion = {
    8
      token: string;
    9
    };
    10
    export async function main(
    11
      auth: Notion,
    12
      block_id: string,
    13
      start_cursor?: string,
    14
      page_size?: number,
    15
    ) {
    16
      const client = new Client({ auth: auth.token });
    17
      const args = removeObjectEmptyFields({
    18
        block_id,
    19
        start_cursor,
    20
        page_size,
    21
      });
    22
      return await client.blocks.children.list(args);
    23
    }
    24
    
    
    25
    function removeObjectEmptyFields(
    26
      object?: Record<string, any>,
    27
      removeEmptyArraysAndObjects = true,
    28
      createNewObject = true,
    29
    ) {
    30
      if (!object || typeof object !== "object") return {}
    31
      const obj = createNewObject ? { ...object } : object
    32
      const emptyValues = [undefined, null, ""]
    33
      for (const key in obj) {
    34
        const value = obj[key]
    35
        if (emptyValues.includes(value)) {
    36
          delete obj[key]
    37
        } else if (typeof value === "object") {
    38
          if (Object.keys(value).length) {
    39
            obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
    40
          }
    41
          if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
    42
            delete obj[key]
    43
          }
    44
        }
    45
      }
    46
      return obj
    47
    }
    48