0

List Groups

by
Published Nov 7, 2023

List the account's agent groups. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.

Script zendesk Verified

The script

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

3
/**
4
 * List Groups
5
 * List the account's agent groups. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  page_size: number | undefined,
10
  cursor: string | undefined
11
) {
12
  const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/groups`)
13
  url.searchParams.append("page[size]", String(page_size || 100))
14
  if (cursor) {
15
    url.searchParams.append("page[after]", cursor)
16
  }
17

18
  const response = await fetch(url, {
19
    headers: {
20
      Authorization: auth.token
21
        ? `Bearer ${auth.token}`
22
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
23
      Accept: "application/json",
24
    },
25
  })
26

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

31
  return await response.json()
32
}
33

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 540 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * List Groups
    8
     * #### Pagination
    9
    
    
    10
    * Cursor pagination (recommended)
    11
    * Offset pagination
    12
    
    
    13
    See Pagination.
    14
    
    
    15
    Returns a maximum of 100 records per page.
    16
    
    
    17
    #### Allowed For
    18
    
    
    19
    * Admins
    20
    * Agents
    21
    
    
    22
     */
    23
    export async function main(auth: Zendesk) {
    24
      const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/groups`);
    25
    
    
    26
      const response = await fetch(url, {
    27
        method: "GET",
    28
        headers: {
    29
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    30
        },
    31
        body: undefined,
    32
      });
    33
      if (!response.ok) {
    34
        const text = await response.text();
    35
        throw new Error(`${response.status} ${text}`);
    36
      }
    37
      return await response.json();
    38
    }
    39