0

Create a New Droplet

by
Published Dec 20, 2024

To create a new Droplet, send a POST request to `/v2/droplets` setting the required attributes.

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 Droplet
7
 * To create a new Droplet, send a POST request to `/v2/droplets` setting the
8
required attributes.
9
 */
10
export async function main(
11
  auth: Digitalocean,
12
  body:
13
    | ({ name: string } & {
14
        region?: string;
15
        size: string;
16
        image: string | number;
17
        ssh_keys?: string | number[];
18
        backups?: false | true;
19
        backup_policy?: {
20
          plan?: "daily" | "weekly";
21
          weekday?: "SUN" | "MON" | "TUE" | "WED" | "THU" | "FRI" | "SAT";
22
          hour?: 0 | 4 | 8 | 12 | 16 | 20;
23
          window_length_hours?: number;
24
          retention_period_days?: number;
25
        } & {};
26
        ipv6?: false | true;
27
        monitoring?: false | true;
28
        tags?: string[];
29
        user_data?: string;
30
        private_networking?: false | true;
31
        volumes?: string[];
32
        vpc_uuid?: string;
33
        with_droplet_agent?: false | true;
34
      })
35
    | ({ names: string[] } & {
36
        region?: string;
37
        size: string;
38
        image: string | number;
39
        ssh_keys?: string | number[];
40
        backups?: false | true;
41
        backup_policy?: {
42
          plan?: "daily" | "weekly";
43
          weekday?: "SUN" | "MON" | "TUE" | "WED" | "THU" | "FRI" | "SAT";
44
          hour?: 0 | 4 | 8 | 12 | 16 | 20;
45
          window_length_hours?: number;
46
          retention_period_days?: number;
47
        } & {};
48
        ipv6?: false | true;
49
        monitoring?: false | true;
50
        tags?: string[];
51
        user_data?: string;
52
        private_networking?: false | true;
53
        volumes?: string[];
54
        vpc_uuid?: string;
55
        with_droplet_agent?: false | true;
56
      }),
57
) {
58
  const url = new URL(`https://api.digitalocean.com/v2/droplets`);
59

60
  const response = await fetch(url, {
61
    method: "POST",
62
    headers: {
63
      "Content-Type": "application/json",
64
      Authorization: "Bearer " + auth.token,
65
    },
66
    body: JSON.stringify(body),
67
  });
68
  if (!response.ok) {
69
    const text = await response.text();
70
    throw new Error(`${response.status} ${text}`);
71
  }
72
  return await response.json();
73
}
74