0

List tickets

by
Published Oct 17, 2025

List tickets, paginated and ordered by the attribute of the given view.

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 tickets
9
 * List tickets, paginated and ordered by the attribute of the given view.
10

11
 */
12
export async function main(
13
  auth: Gorgias,
14
  cursor: string | undefined,
15
  view_id: string | undefined,
16
  customer_id: string | undefined,
17
  page: string | undefined,
18
  external_id: string | undefined,
19
  per_page: string | undefined,
20
  limit: string | undefined,
21
  order_by: "created_datetime:asc" | "created_datetime:desc" | undefined,
22
) {
23
  const url = new URL(`https://${auth.domain}.gorgias.com/api/tickets`);
24
  for (const [k, v] of [
25
    ["cursor", cursor],
26
    ["view_id", view_id],
27
    ["customer_id", customer_id],
28
    ["page", page],
29
    ["external_id", external_id],
30
    ["per_page", per_page],
31
    ["limit", limit],
32
    ["order_by", order_by],
33
  ]) {
34
    if (v !== undefined && v !== "" && k !== undefined) {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51