0

List Macros

by
Published Nov 7, 2023

List the account's macros, active and inactive. Offset-paged (`page`, up to 100 per page).

Script zendesk Verified

The script

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

3
/**
4
 * List Macros
5
 * List the account's macros, active and inactive. Offset-paged (`page`, up to 100 per page).
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  active: "true" | "false" | undefined,
10
  access: "personal" | "agents" | "shared" | "account" | undefined,
11
  category: number | undefined,
12
  group_id: number | undefined,
13
  sort_by:
14
    | "alphabetical"
15
    | "created_at"
16
    | "updated_at"
17
    | "usage_1h"
18
    | "usage_24h"
19
    | "usage_7d"
20
    | "usage_30d"
21
    | undefined,
22
  sort_order: "asc" | "desc" | undefined,
23
  page: number | undefined,
24
  per_page: number | undefined
25
) {
26
  const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/macros`)
27
  const params = {
28
    active,
29
    access,
30
    category,
31
    group_id,
32
    sort_by,
33
    sort_order,
34
    page: page || 1,
35
    per_page: per_page || 100,
36
  }
37
  for (const [key, value] of Object.entries(params)) {
38
    if (value != null && value !== "") {
39
      url.searchParams.append(key, String(value))
40
    }
41
  }
42

43
  const response = await fetch(url, {
44
    headers: {
45
      Authorization: auth.token
46
        ? `Bearer ${auth.token}`
47
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
48
      Accept: "application/json",
49
    },
50
  })
51

52
  if (!response.ok) {
53
    throw new Error(`${response.status} ${await response.text()}`)
54
  }
55

56
  return await response.json()
57
}
58

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 Macros
    8
     * Lists all shared and personal macros available to the current user. For admins, the API returns all macros for the account, including the personal macros of agents and other admins.
    9
    
    
    10
    #### Pagination
    11
    
    
    12
    - Cursor pagination (recommended)
    13
    - Offset pagination
    14
    
    
    15
    See Pagination.
    16
    
    
    17
    Returns a maximum of 100 records per page.
    18
    
    
    19
    #### Allowed For
    20
    * Agents
    21
    
    
    22
     */
    23
    export async function main(
    24
      auth: Zendesk,
    25
      include: string | undefined,
    26
      access: string | undefined,
    27
      active: string | undefined,
    28
      category: string | undefined,
    29
      group_id: string | undefined,
    30
      only_viewable: string | undefined,
    31
      sort_by: string | undefined,
    32
      sort_order: string | undefined
    33
    ) {
    34
      const url = new URL(`https://${auth.subdomain}.zendesk.com/api/v2/macros`);
    35
      for (const [k, v] of [
    36
        ["include", include],
    37
        ["access", access],
    38
        ["active", active],
    39
        ["category", category],
    40
        ["group_id", group_id],
    41
        ["only_viewable", only_viewable],
    42
        ["sort_by", sort_by],
    43
        ["sort_order", sort_order],
    44
      ]) {
    45
        if (v !== undefined && v !== "") {
    46
          url.searchParams.append(k, v);
    47
        }
    48
      }
    49
      const response = await fetch(url, {
    50
        method: "GET",
    51
        headers: {
    52
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    53
        },
    54
        body: undefined,
    55
      });
    56
      if (!response.ok) {
    57
        const text = await response.text();
    58
        throw new Error(`${response.status} ${text}`);
    59
      }
    60
      return await response.json();
    61
    }
    62