Get Speed Tests Summary

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