0

List Users

by
Published Apr 25, 2023

List the workspace's people and bots.

Script notion Verified

The script

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

3
/**
4
 * List Users
5
 * List the workspace's people and bots. Paginate with `next_cursor` as `start_cursor`. Guests are not included.
6
 */
7
export async function main(
8
  auth: RT.Notion,
9
  start_cursor: string | undefined,
10
  page_size: number | undefined
11
) {
12
  const url = new URL("https://api.notion.com/v1/users")
13
  if (start_cursor !== undefined && start_cursor !== "") {
14
    url.searchParams.append("start_cursor", start_cursor)
15
  }
16
  if (page_size !== undefined) {
17
    url.searchParams.append("page_size", String(page_size))
18
  }
19

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

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

32
  return await response.json()
33
}
34

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