0

Update a rule

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
 * Update a rule
9
 *
10
 */
11
export async function main(
12
  auth: Gorgias,
13
  id: string,
14
  body: {
15
    code?: string;
16
    code_ast?: {};
17
    deactivated_datetime?: string;
18
    description?: string;
19
    event_types?:
20
      | "ticket-created"
21
      | "ticket-updated"
22
      | "ticket-message-created"
23
      | "ticket-assigned"
24
      | "ticket-self-unsnoozed";
25
    name?: string;
26
    priority?: number;
27
  },
28
) {
29
  const url = new URL(`https://${auth.domain}.gorgias.com/api/rules/${id}/`);
30

31
  const response = await fetch(url, {
32
    method: "PUT",
33
    headers: {
34
      "Content-Type": "application/json",
35
      Authorization: "Basic " + btoa(`${auth.username}:${auth.apiKey}`),
36
    },
37
    body: JSON.stringify(body),
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