Get Top User Agents By HTTP requests

Get the top user agents by HTTP traffic. Values are a percentage out of the total traffic.

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 Top User Agents By HTTP requests
8
 * Get the top user agents by HTTP traffic. Values are a percentage out of the total traffic.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  limit: string | undefined,
13
  name: string | undefined,
14
  dateRange: string | undefined,
15
  dateStart: string | undefined,
16
  dateEnd: string | undefined,
17
  asn: string | undefined,
18
  location: string | undefined,
19
  botClass: 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/browsers`
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
    ["botClass", botClass],
40
    ["deviceType", deviceType],
41
    ["httpProtocol", httpProtocol],
42
    ["httpVersion", httpVersion],
43
    ["ipVersion", ipVersion],
44
    ["os", os],
45
    ["tlsVersion", tlsVersion],
46
    ["format", format],
47
  ]) {
48
    if (v !== undefined && v !== "") {
49
      url.searchParams.append(k, v);
50
    }
51
  }
52
  const response = await fetch(url, {
53
    method: "GET",
54
    headers: {
55
      "X-AUTH-EMAIL": auth.email,
56
      "X-AUTH-KEY": auth.key,
57
      Authorization: "Bearer " + auth.token,
58
    },
59
    body: undefined,
60
  });
61
  if (!response.ok) {
62
    const text = await response.text();
63
    throw new Error(`${response.status} ${text}`);
64
  }
65
  return await response.json();
66
}
67