Search Users

Returns an array of users who meet the search criteria. #### Pagination * Offset pagination only See Using Offset Pagination. #### Allowed For * Admins, Agents and Light Agents

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
 * Search Users
8
 * Returns an array of users who meet the search criteria.
9

10
#### Pagination
11

12
* Offset pagination only
13

14
See Using Offset Pagination.
15

16
#### Allowed For
17

18
* Admins, Agents and Light Agents
19

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