Get list of ASNs ordered by prefix count

Get the full list of autonomous systems on the global routing table ordered by announced prefixes count. The data comes from public BGP MRT data archives and updates every 2 hours.

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 list of ASNs ordered by prefix count
8
 * Get the full list of autonomous systems on the global routing table ordered by announced prefixes count. The data comes from public BGP MRT data archives and updates every 2 hours.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  country: string | undefined,
13
  limit: string | undefined,
14
  format: "JSON" | "CSV" | undefined
15
) {
16
  const url = new URL(
17
    `https://api.cloudflare.com/client/v4/radar/bgp/top/ases/prefixes`
18
  );
19
  for (const [k, v] of [
20
    ["country", country],
21
    ["limit", limit],
22
    ["format", format],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      "X-AUTH-EMAIL": auth.email,
32
      "X-AUTH-KEY": auth.key,
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43