Count Users

Returns an approximate count of users.

Script zendesk Verified

by hugo697 ยท 11/7/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 377 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * Count Users
8
 * Returns an approximate count of users.
9
 */
10
export async function main(
11
  auth: Zendesk,
12
  role: string | undefined,
13
  role__: string | undefined,
14
  permission_set: string | undefined
15
) {
16
  const url = new URL(
17
    `https://${auth.subdomain}.zendesk.com/api/v2/users/count`
18
  );
19
  for (const [k, v] of [
20
    ["role", role],
21
    ["role[]", role__],
22
    ["permission_set", permission_set],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41