0

List widgets

by
Published Oct 17, 2025

List all widgets for the account, ordered.

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 widgets
9
 * List all widgets for the account, ordered.
10
 */
11
export async function main(
12
  auth: Gorgias,
13
  cursor: string | undefined,
14
  limit: string | undefined,
15
  order_by:
16
    | "created_datetime:asc"
17
    | "created_datetime:desc"
18
    | "order:asc"
19
    | "order:desc"
20
    | undefined,
21
) {
22
  const url = new URL(`https://${auth.domain}.gorgias.com/api/widgets`);
23
  for (const [k, v] of [
24
    ["cursor", cursor],
25
    ["limit", limit],
26
    ["order_by", order_by],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45