Get percentiles for a traceroute test

Get percentiles for a traceroute test for a given time period between 1 hour and 7 days.

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 percentiles for a traceroute test
8
 * Get percentiles for a traceroute test for a given time period between 1 hour and 7 days.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  test_id: string,
14
  deviceId: string | undefined,
15
  timeStart: string | undefined,
16
  timeEnd: string | undefined,
17
  colo: string | undefined
18
) {
19
  const url = new URL(
20
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/dex/traceroute-tests/${test_id}/percentiles`
21
  );
22
  for (const [k, v] of [
23
    ["deviceId", deviceId],
24
    ["timeStart", timeStart],
25
    ["timeEnd", timeEnd],
26
    ["colo", colo],
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