0

Create a New Floating IP

by
Published Dec 20, 2024

On creation, a floating IP must be either assigned to a Droplet or reserved to a region. * To create a new floating IP assigned to a Droplet, send a POST request to `/v2/floating_ips` with the `droplet_id` attribute. * To create a new floating IP reserved to a region, send a POST request to `/v2/floating_ips` with the `region` attribute. **Note**: In addition to the standard rate limiting, only 12 floating IPs may be created per 60 seconds.

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 Floating IP
7
 * On creation, a floating IP must be either assigned to a Droplet or reserved to a region.
8
* To create a new floating IP assigned to a Droplet, send a POST
9
  request to `/v2/floating_ips` with the `droplet_id` attribute.
10

11
* To create a new floating IP reserved to a region, send a POST request to
12
  `/v2/floating_ips` with the `region` attribute.
13

14
**Note**:  In addition to the standard rate limiting, only 12 floating IPs may be created per 60 seconds.
15
 */
16
export async function main(
17
  auth: Digitalocean,
18
  body: { droplet_id: number } | { region: string; project_id?: string },
19
) {
20
  const url = new URL(`https://api.digitalocean.com/v2/floating_ips`);
21

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