0

Update a Domain Record

by
Published Dec 20, 2024

To update an existing record, send a PUT request to `/v2/domains/$DOMAIN_NAME/records/$DOMAIN_RECORD_ID`. Any attribute valid for the record type can be set to a new value for the record. See the [attribute table](#tag/Domain-Records) for details regarding record types and their respective 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
 * Update a Domain Record
7
 * To update an existing record, send a PUT request to
8
`/v2/domains/$DOMAIN_NAME/records/$DOMAIN_RECORD_ID`. Any attribute valid for
9
the record type can be set to a new value for the record.
10

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

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

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