0

Get Top Autonomous Systems By Bot Class

by
Published Nov 16, 2023

Get the top autonomous systems (AS), by HTTP traffic, of the requested bot class. These two categories use Cloudflare's bot score - refer to [Bot Scores](https://developers.cloudflare.com/bots/concepts/bot-score) for more information. Values are a percentage out of the total traffic.

Script cloudflare Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get Top Autonomous Systems By Bot Class
8
 * Get the top autonomous systems (AS), by HTTP traffic, of the requested bot class. These two categories use Cloudflare's bot score - refer to [Bot Scores](https://developers.cloudflare.com/bots/concepts/bot-score) for more information. Values are a percentage out of the total traffic.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  bot_class: "LIKELY_AUTOMATED" | "LIKELY_HUMAN",
13
  limit: string | undefined,
14
  name: string | undefined,
15
  dateRange: string | undefined,
16
  dateStart: string | undefined,
17
  dateEnd: string | undefined,
18
  asn: string | undefined,
19
  location: string | undefined,
20
  deviceType: string | undefined,
21
  httpProtocol: string | undefined,
22
  httpVersion: string | undefined,
23
  ipVersion: string | undefined,
24
  os: string | undefined,
25
  tlsVersion: string | undefined,
26
  format: "JSON" | "CSV" | undefined
27
) {
28
  const url = new URL(
29
    `https://api.cloudflare.com/client/v4/radar/http/top/ases/bot_class/${bot_class}`
30
  );
31
  for (const [k, v] of [
32
    ["limit", limit],
33
    ["name", name],
34
    ["dateRange", dateRange],
35
    ["dateStart", dateStart],
36
    ["dateEnd", dateEnd],
37
    ["asn", asn],
38
    ["location", location],
39
    ["deviceType", deviceType],
40
    ["httpProtocol", httpProtocol],
41
    ["httpVersion", httpVersion],
42
    ["ipVersion", ipVersion],
43
    ["os", os],
44
    ["tlsVersion", tlsVersion],
45
    ["format", format],
46
  ]) {
47
    if (v !== undefined && v !== "") {
48
      url.searchParams.append(k, v);
49
    }
50
  }
51
  const response = await fetch(url, {
52
    method: "GET",
53
    headers: {
54
      "X-AUTH-EMAIL": auth.email,
55
      "X-AUTH-KEY": auth.key,
56
      Authorization: "Bearer " + auth.token,
57
    },
58
    body: undefined,
59
  });
60
  if (!response.ok) {
61
    const text = await response.text();
62
    throw new Error(`${response.status} ${text}`);
63
  }
64
  return await response.json();
65
}
66