Delete a tunnel route (CIDR Endpoint)

Deletes a private network route from an account. The CIDR in `ip_network_encoded` must be written in URL-encoded format. If no virtual_network_id is provided it will delete the route from the default vnet. If no tun_type is provided it will fetch the type from the tunnel_id or if that is missing it will assume Cloudflare Tunnel as default. If tunnel_id is provided it will delete the route from that tunnel, otherwise it will delete the route based on the vnet and tun_type.

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
 * Delete a tunnel route (CIDR Endpoint)
8
 * Deletes a private network route from an account. The CIDR in `ip_network_encoded` must be written in URL-encoded format. If no virtual_network_id is provided it will delete the route from the default vnet. If no tun_type is provided it will fetch the type from the tunnel_id or if that is missing it will assume Cloudflare Tunnel as default. If tunnel_id is provided it will delete the route from that tunnel, otherwise it will delete the route based on the vnet and tun_type.
9

10
 */
11
export async function main(
12
  auth: Cloudflare,
13
  ip_network_encoded: string,
14
  account_identifier: string,
15
  virtual_network_id: string | undefined,
16
  tun_type:
17
    | "cfd_tunnel"
18
    | "warp_connector"
19
    | "ip_sec"
20
    | "gre"
21
    | "cni"
22
    | undefined,
23
  tunnel_id: string | undefined
24
) {
25
  const url = new URL(
26
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/teamnet/routes/network/${ip_network_encoded}`
27
  );
28
  for (const [k, v] of [
29
    ["virtual_network_id", virtual_network_id],
30
    ["tun_type", tun_type],
31
    ["tunnel_id", tunnel_id],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "DELETE",
39
    headers: {
40
      "X-AUTH-EMAIL": auth.email,
41
      "X-AUTH-KEY": auth.key,
42
      Authorization: "Bearer " + auth.token,
43
    },
44
    body: undefined,
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52