0

Create a New VPC

by
Published Dec 20, 2024

To create a VPC, send a POST request to `/v2/vpcs` specifying the attributes in the table below in the JSON body. **Note:** If you do not currently have a VPC network in a specific datacenter region, the first one that you create will be set as the default for that region. The default VPC for a region cannot be changed or deleted.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create a New VPC
7
 * To create a VPC, send a POST request to `/v2/vpcs` specifying the attributes
8
in the table below in the JSON body.
9

10
**Note:** If you do not currently have a VPC network in a specific datacenter
11
region, the first one that you create will be set as the default for that
12
region. The default VPC for a region cannot be changed or deleted.
13

14
 */
15
export async function main(
16
  auth: Digitalocean,
17
  body: { name?: string; description?: string } & {
18
    region?: string;
19
    ip_range?: string;
20
  },
21
) {
22
  const url = new URL(`https://api.digitalocean.com/v2/vpcs`);
23

24
  const response = await fetch(url, {
25
    method: "POST",
26
    headers: {
27
      "Content-Type": "application/json",
28
      Authorization: "Bearer " + auth.token,
29
    },
30
    body: JSON.stringify(body),
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38