Get Top Speed Test Locations

Get the top locations by bandwidth, latency, jitter or packet loss, from the previous 90 days of Cloudflare Speed Test data.

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 Speed Test Locations
8
 * Get the top locations by bandwidth, latency, jitter or packet loss, from the previous 90 days of Cloudflare Speed Test data.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  limit: string | undefined,
13
  name: string | undefined,
14
  dateEnd: string | undefined,
15
  asn: string | undefined,
16
  location: string | undefined,
17
  orderBy:
18
    | "BANDWIDTH_DOWNLOAD"
19
    | "BANDWIDTH_UPLOAD"
20
    | "LATENCY_IDLE"
21
    | "LATENCY_LOADED"
22
    | "JITTER_IDLE"
23
    | "JITTER_LOADED"
24
    | undefined,
25
  reverse: string | undefined,
26
  format: "JSON" | "CSV" | undefined
27
) {
28
  const url = new URL(
29
    `https://api.cloudflare.com/client/v4/radar/quality/speed/top/locations`
30
  );
31
  for (const [k, v] of [
32
    ["limit", limit],
33
    ["name", name],
34
    ["dateEnd", dateEnd],
35
    ["asn", asn],
36
    ["location", location],
37
    ["orderBy", orderBy],
38
    ["reverse", reverse],
39
    ["format", format],
40
  ]) {
41
    if (v !== undefined && v !== "") {
42
      url.searchParams.append(k, v);
43
    }
44
  }
45
  const response = await fetch(url, {
46
    method: "GET",
47
    headers: {
48
      "X-AUTH-EMAIL": auth.email,
49
      "X-AUTH-KEY": auth.key,
50
      Authorization: "Bearer " + auth.token,
51
    },
52
    body: undefined,
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.json();
59
}
60