Create Monitor

Create a configured monitor.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create Monitor
8
 * Create a configured monitor.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  body: {
13
    allow_insecure?: boolean;
14
    consecutive_down?: number;
15
    consecutive_up?: number;
16
    description?: string;
17
    expected_body?: string;
18
    expected_codes?: string;
19
    follow_redirects?: boolean;
20
    header?: { [k: string]: unknown };
21
    interval?: number;
22
    method?: string;
23
    path?: string;
24
    port?: number;
25
    probe_zone?: string;
26
    retries?: number;
27
    timeout?: number;
28
    type?: "http" | "https" | "tcp" | "udp_icmp" | "icmp_ping" | "smtp";
29
    [k: string]: unknown;
30
  } & { [k: string]: unknown }
31
) {
32
  const url = new URL(
33
    `https://api.cloudflare.com/client/v4/user/load_balancers/monitors`
34
  );
35

36
  const response = await fetch(url, {
37
    method: "POST",
38
    headers: {
39
      "X-AUTH-EMAIL": auth.email,
40
      "X-AUTH-KEY": auth.key,
41
      "Content-Type": "application/json",
42
      Authorization: "Bearer " + auth.token,
43
    },
44
    body: JSON.stringify(body),
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52