0

List jobs

by
Published Oct 17, 2025

List jobs, paginated and ordered by their creation date, with the most recent ones 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 jobs
9
 * List jobs, paginated and ordered by their creation date, with the most recent ones first.
10

11
 */
12
export async function main(
13
  auth: Gorgias,
14
  cursor: string | undefined,
15
  _type:
16
    | "applyMacro"
17
    | "deleteTicket"
18
    | "exportTicket"
19
    | "importMacro"
20
    | "exportMacro"
21
    | "updateTicket"
22
    | undefined,
23
  status:
24
    | "cancel_requested"
25
    | "canceled"
26
    | "done"
27
    | "errored"
28
    | "fatal_errored"
29
    | "pending"
30
    | "running"
31
    | "scheduled"
32
    | undefined,
33
  page: string | undefined,
34
  per_page: string | undefined,
35
  limit: string | undefined,
36
  order_by: "created_datetime:asc" | "created_datetime:desc" | undefined,
37
) {
38
  const url = new URL(`https://${auth.domain}.gorgias.com/api/jobs`);
39
  for (const [k, v] of [
40
    ["cursor", cursor],
41
    ["type", _type],
42
    ["status", status],
43
    ["page", page],
44
    ["per_page", per_page],
45
    ["limit", limit],
46
    ["order_by", order_by],
47
  ]) {
48
    if (v !== undefined && v !== "" && k !== undefined) {
49
      url.searchParams.append(k, v);
50
    }
51
  }
52
  const response = await fetch(url, {
53
    method: "GET",
54
    headers: {
55
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
56
    },
57
    body: undefined,
58
  });
59
  if (!response.ok) {
60
    const text = await response.text();
61
    throw new Error(`${response.status} ${text}`);
62
  }
63
  return await response.json();
64
}
65