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

Create a new DNS record for a zone.

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

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