0
Get BGP time series
One script reply has been approved by the moderators Verified

Gets BGP updates change over time. Raw values are returned. When requesting updates of an autonomous system (AS), only BGP updates of type announcement are returned.

Created by hugo697 306 days ago Viewed 8983 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 306 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get BGP time series
8
 * Gets BGP updates change over time. Raw values are returned. When requesting updates of an autonomous system (AS), only BGP updates of type announcement are returned.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  aggInterval: "15m" | "1h" | "1d" | "1w" | undefined,
13
  name: string | undefined,
14
  dateRange: string | undefined,
15
  dateStart: string | undefined,
16
  dateEnd: string | undefined,
17
  prefix: string | undefined,
18
  updateType: string | undefined,
19
  asn: string | undefined,
20
  format: "JSON" | "CSV" | undefined
21
) {
22
  const url = new URL(
23
    `https://api.cloudflare.com/client/v4/radar/bgp/timeseries`
24
  );
25
  for (const [k, v] of [
26
    ["aggInterval", aggInterval],
27
    ["name", name],
28
    ["dateRange", dateRange],
29
    ["dateStart", dateStart],
30
    ["dateEnd", dateEnd],
31
    ["prefix", prefix],
32
    ["updateType", updateType],
33
    ["asn", asn],
34
    ["format", format],
35
  ]) {
36
    if (v !== undefined && v !== "") {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      "X-AUTH-EMAIL": auth.email,
44
      "X-AUTH-KEY": auth.key,
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: undefined,
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55