0
Update DNS Record
One script reply has been approved by the moderators Verified

Update an existing DNS record.

Notes:

  • A/AAAA records cannot exist on the same name as CNAME records.
  • NS records cannot exist on the same name as any other record type.
  • Domain names are always represented in Punycode, even if Unicode characters were used when creating the record.
Created by hugo697 151 days ago Viewed 3737 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 151 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Update DNS Record
8
 * Update an existing DNS record.
9

10
Notes:
11
- A/AAAA records cannot exist on the same name as CNAME records.
12
- NS records cannot exist on the same name as any other record type.
13
- Domain names are always represented in Punycode, even if Unicode
14
  characters were used when creating the record.
15

16
 */
17
export async function main(
18
  auth: Cloudflare,
19
  identifier: string,
20
  zone_identifier: string,
21
  body:
22
    | ({
23
        content?: string;
24
        name?: string;
25
        proxied?: boolean;
26
        type?: "A";
27
        [k: string]: unknown;
28
      } & {
29
        comment?: string;
30
        created_on?: string;
31
        id?: string;
32
        locked?: boolean;
33
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
34
        modified_on?: string;
35
        proxiable?: boolean;
36
        tags?: string[];
37
        ttl?: (number | 1) & number;
38
        zone_id?: string;
39
        zone_name?: string;
40
        [k: string]: unknown;
41
      })
42
    | ({
43
        content?: string;
44
        name?: string;
45
        proxied?: boolean;
46
        type?: "AAAA";
47
        [k: string]: unknown;
48
      } & {
49
        comment?: string;
50
        created_on?: string;
51
        id?: string;
52
        locked?: boolean;
53
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
54
        modified_on?: string;
55
        proxiable?: boolean;
56
        tags?: string[];
57
        ttl?: (number | 1) & number;
58
        zone_id?: string;
59
        zone_name?: string;
60
        [k: string]: unknown;
61
      })
62
    | ({
63
        content?: string;
64
        data?: {
65
          flags?: number;
66
          tag?: string;
67
          value?: string;
68
          [k: string]: unknown;
69
        };
70
        name?: string;
71
        type?: "CAA";
72
        [k: string]: unknown;
73
      } & {
74
        comment?: string;
75
        created_on?: string;
76
        id?: string;
77
        locked?: boolean;
78
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
79
        modified_on?: string;
80
        proxiable?: boolean;
81
        tags?: string[];
82
        ttl?: (number | 1) & number;
83
        zone_id?: string;
84
        zone_name?: string;
85
        [k: string]: unknown;
86
      })
87
    | ({
88
        content?: string;
89
        data?: {
90
          algorithm?: number;
91
          certificate?: string;
92
          key_tag?: number;
93
          type?: number;
94
          [k: string]: unknown;
95
        };
96
        name?: string;
97
        type?: "CERT";
98
        [k: string]: unknown;
99
      } & {
100
        comment?: string;
101
        created_on?: string;
102
        id?: string;
103
        locked?: boolean;
104
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
105
        modified_on?: string;
106
        proxiable?: boolean;
107
        tags?: string[];
108
        ttl?: (number | 1) & number;
109
        zone_id?: string;
110
        zone_name?: string;
111
        [k: string]: unknown;
112
      })
113
    | ({
114
        content?: { [k: string]: unknown };
115
        name?: string;
116
        proxied?: boolean;
117
        type?: "CNAME";
118
        [k: string]: unknown;
119
      } & {
120
        comment?: string;
121
        created_on?: string;
122
        id?: string;
123
        locked?: boolean;
124
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
125
        modified_on?: string;
126
        proxiable?: boolean;
127
        tags?: string[];
128
        ttl?: (number | 1) & number;
129
        zone_id?: string;
130
        zone_name?: string;
131
        [k: string]: unknown;
132
      })
133
    | ({
134
        content?: string;
135
        data?: {
136
          algorithm?: number;
137
          flags?: number;
138
          protocol?: number;
139
          public_key?: string;
140
          [k: string]: unknown;
141
        };
142
        name?: string;
143
        type?: "DNSKEY";
144
        [k: string]: unknown;
145
      } & {
146
        comment?: string;
147
        created_on?: string;
148
        id?: string;
149
        locked?: boolean;
150
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
151
        modified_on?: string;
152
        proxiable?: boolean;
153
        tags?: string[];
154
        ttl?: (number | 1) & number;
155
        zone_id?: string;
156
        zone_name?: string;
157
        [k: string]: unknown;
158
      })
159
    | ({
160
        content?: string;
161
        data?: {
162
          algorithm?: number;
163
          digest?: string;
164
          digest_type?: number;
165
          key_tag?: number;
166
          [k: string]: unknown;
167
        };
168
        name?: string;
169
        type?: "DS";
170
        [k: string]: unknown;
171
      } & {
172
        comment?: string;
173
        created_on?: string;
174
        id?: string;
175
        locked?: boolean;
176
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
177
        modified_on?: string;
178
        proxiable?: boolean;
179
        tags?: string[];
180
        ttl?: (number | 1) & number;
181
        zone_id?: string;
182
        zone_name?: string;
183
        [k: string]: unknown;
184
      })
185
    | ({
186
        content?: string;
187
        data?: {
188
          priority?: number;
189
          target?: string;
190
          value?: string;
191
          [k: string]: unknown;
192
        };
193
        name?: string;
194
        type?: "HTTPS";
195
        [k: string]: unknown;
196
      } & {
197
        comment?: string;
198
        created_on?: string;
199
        id?: string;
200
        locked?: boolean;
201
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
202
        modified_on?: string;
203
        proxiable?: boolean;
204
        tags?: string[];
205
        ttl?: (number | 1) & number;
206
        zone_id?: string;
207
        zone_name?: string;
208
        [k: string]: unknown;
209
      })
210
    | ({
211
        content?: string;
212
        data?: {
213
          altitude?: number;
214
          lat_degrees?: number;
215
          lat_direction?: "N" | "S";
216
          lat_minutes?: number;
217
          lat_seconds?: number;
218
          long_degrees?: number;
219
          long_direction?: "E" | "W";
220
          long_minutes?: number;
221
          long_seconds?: number;
222
          precision_horz?: number;
223
          precision_vert?: number;
224
          size?: number;
225
          [k: string]: unknown;
226
        };
227
        name?: string;
228
        type?: "LOC";
229
        [k: string]: unknown;
230
      } & {
231
        comment?: string;
232
        created_on?: string;
233
        id?: string;
234
        locked?: boolean;
235
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
236
        modified_on?: string;
237
        proxiable?: boolean;
238
        tags?: string[];
239
        ttl?: (number | 1) & number;
240
        zone_id?: string;
241
        zone_name?: string;
242
        [k: string]: unknown;
243
      })
244
    | ({
245
        content?: string;
246
        name?: string;
247
        priority?: number;
248
        type?: "MX";
249
        [k: string]: unknown;
250
      } & {
251
        comment?: string;
252
        created_on?: string;
253
        id?: string;
254
        locked?: boolean;
255
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
256
        modified_on?: string;
257
        proxiable?: boolean;
258
        tags?: string[];
259
        ttl?: (number | 1) & number;
260
        zone_id?: string;
261
        zone_name?: string;
262
        [k: string]: unknown;
263
      })
264
    | ({
265
        content?: string;
266
        data?: {
267
          flags?: string;
268
          order?: number;
269
          preference?: number;
270
          regex?: string;
271
          replacement?: string;
272
          service?: string;
273
          [k: string]: unknown;
274
        };
275
        name?: string;
276
        type?: "NAPTR";
277
        [k: string]: unknown;
278
      } & {
279
        comment?: string;
280
        created_on?: string;
281
        id?: string;
282
        locked?: boolean;
283
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
284
        modified_on?: string;
285
        proxiable?: boolean;
286
        tags?: string[];
287
        ttl?: (number | 1) & number;
288
        zone_id?: string;
289
        zone_name?: string;
290
        [k: string]: unknown;
291
      })
292
    | ({
293
        content?: { [k: string]: unknown };
294
        name?: string;
295
        type?: "NS";
296
        [k: string]: unknown;
297
      } & {
298
        comment?: string;
299
        created_on?: string;
300
        id?: string;
301
        locked?: boolean;
302
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
303
        modified_on?: string;
304
        proxiable?: boolean;
305
        tags?: string[];
306
        ttl?: (number | 1) & number;
307
        zone_id?: string;
308
        zone_name?: string;
309
        [k: string]: unknown;
310
      })
311
    | ({
312
        content?: string;
313
        name?: string;
314
        type?: "PTR";
315
        [k: string]: unknown;
316
      } & {
317
        comment?: string;
318
        created_on?: string;
319
        id?: string;
320
        locked?: boolean;
321
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
322
        modified_on?: string;
323
        proxiable?: boolean;
324
        tags?: string[];
325
        ttl?: (number | 1) & number;
326
        zone_id?: string;
327
        zone_name?: string;
328
        [k: string]: unknown;
329
      })
330
    | ({
331
        content?: string;
332
        data?: {
333
          certificate?: string;
334
          matching_type?: number;
335
          selector?: number;
336
          usage?: number;
337
          [k: string]: unknown;
338
        };
339
        name?: string;
340
        type?: "SMIMEA";
341
        [k: string]: unknown;
342
      } & {
343
        comment?: string;
344
        created_on?: string;
345
        id?: string;
346
        locked?: boolean;
347
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
348
        modified_on?: string;
349
        proxiable?: boolean;
350
        tags?: string[];
351
        ttl?: (number | 1) & number;
352
        zone_id?: string;
353
        zone_name?: string;
354
        [k: string]: unknown;
355
      })
356
    | ({
357
        content?: string;
358
        data?: {
359
          name?: string;
360
          port?: number;
361
          priority?: number;
362
          proto?: string;
363
          service?: string;
364
          target?: string;
365
          weight?: number;
366
          [k: string]: unknown;
367
        };
368
        name?: string;
369
        type?: "SRV";
370
        [k: string]: unknown;
371
      } & {
372
        comment?: string;
373
        created_on?: string;
374
        id?: string;
375
        locked?: boolean;
376
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
377
        modified_on?: string;
378
        proxiable?: boolean;
379
        tags?: string[];
380
        ttl?: (number | 1) & number;
381
        zone_id?: string;
382
        zone_name?: string;
383
        [k: string]: unknown;
384
      })
385
    | ({
386
        content?: string;
387
        data?: {
388
          algorithm?: number;
389
          fingerprint?: string;
390
          type?: number;
391
          [k: string]: unknown;
392
        };
393
        name?: string;
394
        type?: "SSHFP";
395
        [k: string]: unknown;
396
      } & {
397
        comment?: string;
398
        created_on?: string;
399
        id?: string;
400
        locked?: boolean;
401
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
402
        modified_on?: string;
403
        proxiable?: boolean;
404
        tags?: string[];
405
        ttl?: (number | 1) & number;
406
        zone_id?: string;
407
        zone_name?: string;
408
        [k: string]: unknown;
409
      })
410
    | ({
411
        content?: string;
412
        data?: {
413
          priority?: number;
414
          target?: string;
415
          value?: string;
416
          [k: string]: unknown;
417
        };
418
        name?: string;
419
        type?: "SVCB";
420
        [k: string]: unknown;
421
      } & {
422
        comment?: string;
423
        created_on?: string;
424
        id?: string;
425
        locked?: boolean;
426
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
427
        modified_on?: string;
428
        proxiable?: boolean;
429
        tags?: string[];
430
        ttl?: (number | 1) & number;
431
        zone_id?: string;
432
        zone_name?: string;
433
        [k: string]: unknown;
434
      })
435
    | ({
436
        content?: string;
437
        data?: {
438
          certificate?: string;
439
          matching_type?: number;
440
          selector?: number;
441
          usage?: number;
442
          [k: string]: unknown;
443
        };
444
        name?: string;
445
        type?: "TLSA";
446
        [k: string]: unknown;
447
      } & {
448
        comment?: string;
449
        created_on?: string;
450
        id?: string;
451
        locked?: boolean;
452
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
453
        modified_on?: string;
454
        proxiable?: boolean;
455
        tags?: string[];
456
        ttl?: (number | 1) & number;
457
        zone_id?: string;
458
        zone_name?: string;
459
        [k: string]: unknown;
460
      })
461
    | ({
462
        content?: string;
463
        name?: string;
464
        type?: "TXT";
465
        [k: string]: unknown;
466
      } & {
467
        comment?: string;
468
        created_on?: string;
469
        id?: string;
470
        locked?: boolean;
471
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
472
        modified_on?: string;
473
        proxiable?: boolean;
474
        tags?: string[];
475
        ttl?: (number | 1) & number;
476
        zone_id?: string;
477
        zone_name?: string;
478
        [k: string]: unknown;
479
      })
480
    | ({
481
        content?: string;
482
        data?: { content?: string; weight?: number; [k: string]: unknown };
483
        name?: string;
484
        priority?: number;
485
        type?: "URI";
486
        [k: string]: unknown;
487
      } & {
488
        comment?: string;
489
        created_on?: string;
490
        id?: string;
491
        locked?: boolean;
492
        meta?: { auto_added?: boolean; source?: string; [k: string]: unknown };
493
        modified_on?: string;
494
        proxiable?: boolean;
495
        tags?: string[];
496
        ttl?: (number | 1) & number;
497
        zone_id?: string;
498
        zone_name?: string;
499
        [k: string]: unknown;
500
      })
501
) {
502
  const url = new URL(
503
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/dns_records/${identifier}`
504
  );
505

506
  const response = await fetch(url, {
507
    method: "PUT",
508
    headers: {
509
      "X-AUTH-EMAIL": auth.email,
510
      "X-AUTH-KEY": auth.key,
511
      "Content-Type": "application/json",
512
      Authorization: "Bearer " + auth.token,
513
    },
514
    body: JSON.stringify(body),
515
  });
516
  if (!response.ok) {
517
    const text = await response.text();
518
    throw new Error(`${response.status} ${text}`);
519
  }
520
  return await response.json();
521
}
522