0

List events

by
Published Oct 17, 2025

List events, paginated, and ordered by their creation date, with the most recently created first.

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 events
9
 * List events, paginated, and ordered by their creation date, with the most recently created first.
10
 */
11
export async function main(
12
  auth: Gorgias,
13
  created_datetime: any,
14
  object_id: string | undefined,
15
  types: string | undefined,
16
  object_type:
17
    | "Account"
18
    | "Macro"
19
    | "Tag"
20
    | "Customer"
21
    | "Team"
22
    | "View"
23
    | "Widget"
24
    | "User"
25
    | "TicketMessage"
26
    | "Ticket"
27
    | "Rule"
28
    | "Integration"
29
    | undefined,
30
  cursor: string | undefined,
31
  order_dir: "desc" | "asc" | undefined,
32
  page: string | undefined,
33
  user_ids: string | undefined,
34
  per_page: string | undefined,
35
  limit: string | undefined,
36
  order_by: "created_datetime" | undefined,
37
) {
38
  const url = new URL(`https://${auth.domain}.gorgias.com/api/events`);
39
  for (const [k, v] of [
40
    ["object_id", object_id],
41
    ["types", types],
42
    ["object_type", object_type],
43
    ["cursor", cursor],
44
    ["order_dir", order_dir],
45
    ["page", page],
46
    ["user_ids", user_ids],
47
    ["per_page", per_page],
48
    ["limit", limit],
49
    ["order_by", order_by],
50
  ]) {
51
    if (v !== undefined && v !== "" && k !== undefined) {
52
      url.searchParams.append(k, v);
53
    }
54
  }
55
  encodeParams({ created_datetime }).forEach((v, k) => {
56
    if (v !== undefined && v !== "") {
57
      url.searchParams.append(k, v);
58
    }
59
  });
60
  const response = await fetch(url, {
61
    method: "GET",
62
    headers: {
63
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
64
    },
65
    body: undefined,
66
  });
67
  if (!response.ok) {
68
    const text = await response.text();
69
    throw new Error(`${response.status} ${text}`);
70
  }
71
  return await response.json();
72
}
73

74
function encodeParams(o: any) {
75
  function iter(o: any, path: string) {
76
    if (Array.isArray(o)) {
77
      o.forEach(function (a) {
78
        iter(a, path + "[]");
79
      });
80
      return;
81
    }
82
    if (o !== null && typeof o === "object") {
83
      Object.keys(o).forEach(function (k) {
84
        iter(o[k], path + "[" + k + "]");
85
      });
86
      return;
87
    }
88
    data.push(path + "=" + o);
89
  }
90
  const data: string[] = [];
91
  Object.keys(o).forEach(function (k) {
92
    if (o[k] !== undefined) {
93
      iter(o[k], k);
94
    }
95
  });
96
  return new URLSearchParams(data.join("&"));
97
}
98