List Users
One script reply has been approved by the moderators Verified

Pagination

  • Cursor pagination (recommended)
  • Offset pagination

See Pagination.

Returns a maximum of 100 records per page.

Allowed For

  • Admins, Agents and Light Agents
Created by hugo697 844 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 298 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * List Users
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, Agents and Light Agents
20

21
 */
22
export async function main(
23
  auth: Zendesk,
24
  role: string | undefined,
25
  role__: string | undefined,
26
  permission_set: string | undefined,
27
  external_id: string | undefined
28
) {
29
  const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/users`);
30
  for (const [k, v] of [
31
    ["role", role],
32
    ["role[]", role__],
33
    ["permission_set", permission_set],
34
    ["external_id", external_id],
35
  ]) {
36
    if (v !== undefined && v !== "") {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53