0

Create a New Domain

by
Published Dec 20, 2024

To create a new domain, send a POST request to `/v2/domains`. Set the "name" attribute to the domain name you are adding. Optionally, you may set the "ip_address" attribute, and an A record will be automatically created pointing to the apex domain.

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 Domain
7
 * To create a new domain, send a POST request to `/v2/domains`. Set the "name"
8
attribute to the domain name you are adding. Optionally, you may set the
9
"ip_address" attribute, and an A record will be automatically created pointing
10
to the apex domain.
11

12
 */
13
export async function main(
14
  auth: Digitalocean,
15
  body: {
16
    name?: string;
17
    ip_address?: string;
18
    ttl?: number;
19
    zone_file?: string;
20
  },
21
) {
22
  const url = new URL(`https://api.digitalocean.com/v2/domains`);
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