0

Create a firewall

by
Published Oct 17, 2025

Creates a Firewall to filter network traffic.

Script linode Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Linode = {
3
  token: string;
4
};
5
/**
6
 * Create a firewall
7
 * Creates a Firewall to filter network traffic.
8
 */
9
export async function main(
10
  auth: Linode,
11
  apiVersion: "v4" | "v4beta",
12
  body: {
13
    created?: string;
14
    id?: number;
15
    label?: string;
16
    rules?: {
17
      fingerprint?: string;
18
      inbound?: {
19
        action?: "ACCEPT" | "DROP";
20
        addresses?: { ipv4?: string[]; ipv6?: string[] };
21
        description?: string;
22
        label?: string;
23
        ports?: string;
24
        protocol?: "TCP" | "UDP" | "ICMP" | "IPENCAP";
25
      }[];
26
      inbound_policy?: "ACCEPT" | "DROP";
27
      outbound?: {
28
        action?: "ACCEPT" | "DROP";
29
        addresses?: { ipv4?: string[]; ipv6?: string[] };
30
        description?: string;
31
        label?: string;
32
        ports?: string;
33
        protocol?: "TCP" | "UDP" | "ICMP" | "IPENCAP";
34
      }[];
35
      outbound_policy?: "ACCEPT" | "DROP";
36
      version?: number;
37
    };
38
    status?: "enabled" | "disabled" | "deleted";
39
    tags?: string[];
40
    updated?: string;
41
  } & {
42
    devices?: { linodes?: number[]; nodebalancers?: number[] };
43
    rules: { inbound?: {}; outbound?: {} };
44
  },
45
) {
46
  const url = new URL(
47
    `https://api.linode.com/${apiVersion}/networking/firewalls`,
48
  );
49

50
  const response = await fetch(url, {
51
    method: "POST",
52
    headers: {
53
      "Content-Type": "application/json",
54
      Authorization: "Bearer " + auth.token,
55
    },
56
    body: JSON.stringify(body),
57
  });
58
  if (!response.ok) {
59
    const text = await response.text();
60
    throw new Error(`${response.status} ${text}`);
61
  }
62
  return await response.json();
63
}
64