0

List Organizations

by
Published Nov 7, 2023

List the account's organizations. 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 5 days ago
1
//native
2

3
/**
4
 * List Organizations
5
 * List the account's organizations. 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(
13
    `https://${auth.subdomain}.zendesk.com/api/v2/organizations`
14
  )
15
  url.searchParams.append("page[size]", String(page_size || 100))
16
  if (cursor) {
17
    url.searchParams.append("page[after]", cursor)
18
  }
19

20
  const response = await fetch(url, {
21
    headers: {
22
      Authorization: auth.token
23
        ? `Bearer ${auth.token}`
24
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
25
      Accept: "application/json",
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 hugo697 Typescript (fetch-only)
    Created 533 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * List Organizations
    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
    * Agents, with certain restrictions
    20
    
    
    21
    If the agent has a custom agent role that restricts their access to only users in their own organization, a 403 Forbidden error is returned. See [Creating custom agent roles](https://support.zendesk.com/hc/en-us/articles/203662026-Creating-custom-roles-and-assigning-agents#topic_cxn_hig_bd) in Zendesk help.
    22
    
    
    23
     */
    24
    export async function main(auth: Zendesk) {
    25
      const url = new URL(
    26
        `https://${auth.subdomain}.zendesk.com/api/v2/organizations`
    27
      );
    28
    
    
    29
      const response = await fetch(url, {
    30
        method: "GET",
    31
        headers: {
    32
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    33
        },
    34
        body: undefined,
    35
      });
    36
      if (!response.ok) {
    37
        const text = await response.text();
    38
        throw new Error(`${response.status} ${text}`);
    39
      }
    40
      return await response.json();
    41
    }
    42