Get latest Internet traffic anomalies.

Internet traffic anomalies are signals that might point to an outage, These alerts are automatically detected by Radar and then manually verified by our team. This endpoint returns the latest alerts.

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
 * Get latest Internet traffic anomalies.
8
 * Internet traffic anomalies are signals that might point to an outage,
9
        These alerts are automatically detected by Radar and then manually verified by our team.
10
        This endpoint returns the latest alerts.
11
        
12
 */
13
export async function main(
14
  auth: Cloudflare,
15
  limit: string | undefined,
16
  offset: string | undefined,
17
  dateRange:
18
    | "1d"
19
    | "2d"
20
    | "7d"
21
    | "14d"
22
    | "28d"
23
    | "12w"
24
    | "24w"
25
    | "52w"
26
    | "1dControl"
27
    | "2dControl"
28
    | "7dControl"
29
    | "14dControl"
30
    | "28dControl"
31
    | "12wControl"
32
    | "24wControl"
33
    | undefined,
34
  dateStart: string | undefined,
35
  dateEnd: string | undefined,
36
  status: "VERIFIED" | "UNVERIFIED" | undefined,
37
  asn: string | undefined,
38
  location: string | undefined,
39
  format: "JSON" | "CSV" | undefined
40
) {
41
  const url = new URL(
42
    `https://api.cloudflare.com/client/v4/radar/traffic_anomalies`
43
  );
44
  for (const [k, v] of [
45
    ["limit", limit],
46
    ["offset", offset],
47
    ["dateRange", dateRange],
48
    ["dateStart", dateStart],
49
    ["dateEnd", dateEnd],
50
    ["status", status],
51
    ["asn", asn],
52
    ["location", location],
53
    ["format", format],
54
  ]) {
55
    if (v !== undefined && v !== "") {
56
      url.searchParams.append(k, v);
57
    }
58
  }
59
  const response = await fetch(url, {
60
    method: "GET",
61
    headers: {
62
      "X-AUTH-EMAIL": auth.email,
63
      "X-AUTH-KEY": auth.key,
64
      Authorization: "Bearer " + auth.token,
65
    },
66
    body: undefined,
67
  });
68
  if (!response.ok) {
69
    const text = await response.text();
70
    throw new Error(`${response.status} ${text}`);
71
  }
72
  return await response.json();
73
}
74