0

Update an Alert

by
Published Dec 20, 2024

To update the settings of an Uptime alert, send a PUT request to `/v2/uptime/checks/$CHECK_ID/alerts/$ALERT_ID`.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Update an Alert
7
 * To update the settings of an Uptime alert, send a PUT request to `/v2/uptime/checks/$CHECK_ID/alerts/$ALERT_ID`.
8

9
 */
10
export async function main(
11
  auth: Digitalocean,
12
  check_id: string,
13
  alert_id: string,
14
  body: {
15
    name?: string;
16
    type?: "latency" | "down" | "down_global" | "ssl_expiry";
17
    threshold?: number;
18
    comparison?: "greater_than" | "less_than";
19
    notifications?: {
20
      email: string[];
21
      slack: { channel: string; url: string }[];
22
    };
23
    period?: "2m" | "3m" | "5m" | "10m" | "15m" | "30m" | "1h";
24
  },
25
) {
26
  const url = new URL(
27
    `https://api.digitalocean.com/v2/uptime/checks/${check_id}/alerts/${alert_id}`,
28
  );
29

30
  const response = await fetch(url, {
31
    method: "PUT",
32
    headers: {
33
      "Content-Type": "application/json",
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44