List All Tunnels

Lists and filters all types of Tunnels in an account.

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
 * List All Tunnels
8
 * Lists and filters all types of Tunnels in an account.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  name: string | undefined,
14
  is_deleted: string | undefined,
15
  existed_at: string | undefined,
16
  uuid: string | undefined,
17
  was_active_at: string | undefined,
18
  was_inactive_at: string | undefined,
19
  include_prefix: string | undefined,
20
  exclude_prefix: string | undefined,
21
  tun_types: string | undefined,
22
  per_page: string | undefined,
23
  page: string | undefined
24
) {
25
  const url = new URL(
26
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/tunnels`
27
  );
28
  for (const [k, v] of [
29
    ["name", name],
30
    ["is_deleted", is_deleted],
31
    ["existed_at", existed_at],
32
    ["uuid", uuid],
33
    ["was_active_at", was_active_at],
34
    ["was_inactive_at", was_inactive_at],
35
    ["include_prefix", include_prefix],
36
    ["exclude_prefix", exclude_prefix],
37
    ["tun_types", tun_types],
38
    ["per_page", per_page],
39
    ["page", page],
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