Create IPsec tunnels
One script reply has been approved by the moderators Verified

Creates new IPsec tunnels associated with an account. Use ?validate_only=true as an optional query parameter to only run validation without persisting changes.

Created by hugo697 847 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 310 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create IPsec tunnels
8
 * Creates new IPsec tunnels associated with an account. Use `?validate_only=true` as an optional query parameter to only run validation without persisting changes.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body: {
14
    cloudflare_endpoint: string;
15
    customer_endpoint?: string;
16
    description?: string;
17
    interface_address: string;
18
    name: string;
19
    psk?: string;
20
    replay_protection?: boolean;
21
    [k: string]: unknown;
22
  }
23
) {
24
  const url = new URL(
25
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/magic/ipsec_tunnels`
26
  );
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      "X-AUTH-EMAIL": auth.email,
32
      "X-AUTH-KEY": auth.key,
33
      "Content-Type": "application/json",
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44