0

Create a New Domain Record

by
Published Dec 20, 2024

To create a new record to a domain, send a POST request to `/v2/domains/$DOMAIN_NAME/records`. The request must include all of the required fields for the domain record type being added. See the [attribute table](#tag/Domain-Records) for details regarding record types and their respective 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 Domain Record
7
 * To create a new record to a domain, send a POST request to
8
`/v2/domains/$DOMAIN_NAME/records`.
9

10
The request must include all of the required fields for the domain record type
11
being added.
12

13
See the [attribute table](#tag/Domain-Records) for details regarding record
14
types and their respective required attributes.
15

16
 */
17
export async function main(
18
  auth: Digitalocean,
19
  domain_name: string,
20
  body: {
21
    id?: number;
22
    type: string;
23
    name?: string;
24
    data?: string;
25
    priority?: number;
26
    port?: number;
27
    ttl?: number;
28
    weight?: number;
29
    flags?: number;
30
    tag?: string;
31
  } & {},
32
) {
33
  const url = new URL(
34
    `https://api.digitalocean.com/v2/domains/${domain_name}/records`,
35
  );
36

37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      "Content-Type": "application/json",
41
      Authorization: "Bearer " + auth.token,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51