0

List macros

by
Published Oct 17, 2025
Script gorgias Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Gorgias = {
3
  username: string;
4
  apiKey: string;
5
  domain: string;
6
};
7
/**
8
 * List macros
9
 *
10
 */
11
export async function main(
12
  auth: Gorgias,
13
  search: string | undefined,
14
  cursor: string | undefined,
15
  order_dir: "asc" | "desc" | undefined,
16
  ticket_id: string | undefined,
17
  page: string | undefined,
18
  message_id: string | undefined,
19
  per_page: string | undefined,
20
  limit: string | undefined,
21
  order_by:
22
    | "name"
23
    | "created_datetime"
24
    | "updated_datetime"
25
    | "usage"
26
    | "relevance"
27
    | "name:asc"
28
    | "created_datetime:desc"
29
    | "updated_datetime:desc"
30
    | "usage:desc"
31
    | "relevance:desc"
32
    | undefined,
33
) {
34
  const url = new URL(`https://${auth.domain}.gorgias.com/api/macros`);
35
  for (const [k, v] of [
36
    ["search", search],
37
    ["cursor", cursor],
38
    ["order_dir", order_dir],
39
    ["ticket_id", ticket_id],
40
    ["page", page],
41
    ["message_id", message_id],
42
    ["per_page", per_page],
43
    ["limit", limit],
44
    ["order_by", order_by],
45
  ]) {
46
    if (v !== undefined && v !== "" && k !== undefined) {
47
      url.searchParams.append(k, v);
48
    }
49
  }
50
  const response = await fetch(url, {
51
    method: "GET",
52
    headers: {
53
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
54
    },
55
    body: undefined,
56
  });
57
  if (!response.ok) {
58
    const text = await response.text();
59
    throw new Error(`${response.status} ${text}`);
60
  }
61
  return await response.json();
62
}
63