0

Create a New VPC Peering

by
Published Dec 20, 2024

To create a new VPC Peering, send a POST request to `/v2/vpc_peerings` specifying a name and a list of two VPC IDs to peer. The response code, 202 Accepted, does not indicate the success or failure of the operation, just that the request has been accepted for processing.

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 Peering
7
 * To create a new VPC Peering, send a POST request to `/v2/vpc_peerings` 
8
specifying a name and a list of two VPC IDs to peer. The response code, 202 
9
Accepted, does not indicate the success or failure of the operation, just 
10
that the request has been accepted for processing.
11

12
 */
13
export async function main(
14
  auth: Digitalocean,
15
  body: { name?: string } & { vpc_ids?: string[] },
16
) {
17
  const url = new URL(`https://api.digitalocean.com/v2/vpc_peerings`);
18

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