0

Search

by
Published Apr 25, 2023

Search the pages and data sources shared with the integration by title.

Script notion Verified

The script

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

3
/**
4
 * Search
5
 * Search the pages and data sources (databases) shared with the integration by title. Leave `query` empty to list everything. Paginate with `next_cursor` as `start_cursor`.
6
 */
7
export async function main(
8
  auth: RT.Notion,
9
  query: string | undefined,
10
  object_type: "page" | "data_source" | undefined,
11
  sort_direction: "ascending" | "descending" | undefined,
12
  start_cursor: string | undefined,
13
  page_size: number | undefined
14
) {
15
  const body: { [key: string]: any } = {}
16
  if (query !== undefined && query !== "") body.query = query
17
  if (object_type) body.filter = { property: "object", value: object_type }
18
  if (sort_direction) {
19
    body.sort = { timestamp: "last_edited_time", direction: sort_direction }
20
  }
21
  if (start_cursor !== undefined && start_cursor !== "") {
22
    body.start_cursor = start_cursor
23
  }
24
  if (page_size !== undefined) body.page_size = page_size
25

26
  const response = await fetch("https://api.notion.com/v1/search", {
27
    method: "POST",
28
    headers: {
29
      Authorization: `Bearer ${auth.token}`,
30
      "Content-Type": "application/json",
31
      Accept: "application/json",
32
      "Notion-Version": "2026-03-11",
33
    },
34
    body: JSON.stringify(body),
35
  })
36

37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40

41
  return await response.json()
42
}
43

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