0

Search Users

by
Published Nov 7, 2023

Find users by name, email, phone or other user properties (`query`, e.g. `[email protected]` or `role:agent`), or by exact `external_id`. Offset-paged (`page`, up to 100 per page).

Script zendesk Verified

The script

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

3
/**
4
 * Search Users
5
 * Find users by name, email, phone or other user properties (`query`, e.g. `jane@example.com` or `role:agent`), or by exact `external_id`. Offset-paged (`page`, up to 100 per page).
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  query: string | undefined,
10
  external_id: string | undefined,
11
  page: number | undefined,
12
  per_page: number | undefined
13
) {
14
  const url = new URL(
15
    `https://${auth.subdomain}.zendesk.com/api/v2/users/search`
16
  )
17
  if (query) {
18
    url.searchParams.append("query", query)
19
  }
20
  if (external_id) {
21
    url.searchParams.append("external_id", external_id)
22
  }
23
  url.searchParams.append("page", String(page || 1))
24
  url.searchParams.append("per_page", String(per_page || 100))
25

26
  const response = await fetch(url, {
27
    headers: {
28
      Authorization: auth.token
29
        ? `Bearer ${auth.token}`
30
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
31
      Accept: "application/json",
32
    },
33
  })
34

35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38

39
  return await response.json()
40
}
41

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 539 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