0

Create a DNS record

by
Published Apr 8, 2025

Creates a DNS record for a domain.

Script vercel Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Vercel = {
3
  token: string;
4
};
5
/**
6
 * Create a DNS record
7
 * Creates a DNS record for a domain.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  domain: string,
12
  teamId: string | undefined,
13
  slug: string | undefined,
14
  body:
15
    | ({
16
        name: string;
17
        type: "A";
18
        ttl?: number;
19
        value: string;
20
        comment?: string;
21
      } & {
22
        type:
23
          | "A"
24
          | "AAAA"
25
          | "ALIAS"
26
          | "CAA"
27
          | "CNAME"
28
          | "HTTPS"
29
          | "MX"
30
          | "SRV"
31
          | "TXT"
32
          | "NS";
33
      })
34
    | ({
35
        name: string;
36
        type: "AAAA";
37
        ttl?: number;
38
        value: string;
39
        comment?: string;
40
      } & {
41
        type:
42
          | "A"
43
          | "AAAA"
44
          | "ALIAS"
45
          | "CAA"
46
          | "CNAME"
47
          | "HTTPS"
48
          | "MX"
49
          | "SRV"
50
          | "TXT"
51
          | "NS";
52
      })
53
    | ({
54
        name: string;
55
        type: "ALIAS";
56
        ttl?: number;
57
        value: string;
58
        comment?: string;
59
      } & {
60
        type:
61
          | "A"
62
          | "AAAA"
63
          | "ALIAS"
64
          | "CAA"
65
          | "CNAME"
66
          | "HTTPS"
67
          | "MX"
68
          | "SRV"
69
          | "TXT"
70
          | "NS";
71
      })
72
    | ({
73
        name: string;
74
        type: "CAA";
75
        ttl?: number;
76
        value: string;
77
        comment?: string;
78
      } & {
79
        type:
80
          | "A"
81
          | "AAAA"
82
          | "ALIAS"
83
          | "CAA"
84
          | "CNAME"
85
          | "HTTPS"
86
          | "MX"
87
          | "SRV"
88
          | "TXT"
89
          | "NS";
90
      })
91
    | ({
92
        name: string;
93
        type: "CNAME";
94
        ttl?: number;
95
        value?: string;
96
        comment?: string;
97
      } & {
98
        type:
99
          | "A"
100
          | "AAAA"
101
          | "ALIAS"
102
          | "CAA"
103
          | "CNAME"
104
          | "HTTPS"
105
          | "MX"
106
          | "SRV"
107
          | "TXT"
108
          | "NS";
109
      })
110
    | ({
111
        name: string;
112
        type: "MX";
113
        ttl?: number;
114
        value: string;
115
        mxPriority: number;
116
        comment?: string;
117
      } & {
118
        type:
119
          | "A"
120
          | "AAAA"
121
          | "ALIAS"
122
          | "CAA"
123
          | "CNAME"
124
          | "HTTPS"
125
          | "MX"
126
          | "SRV"
127
          | "TXT"
128
          | "NS";
129
      })
130
    | ({
131
        name: string;
132
        type: "SRV";
133
        ttl?: number;
134
        srv: { priority: number; weight: number; port: number; target: string };
135
        comment?: string;
136
      } & {
137
        type:
138
          | "A"
139
          | "AAAA"
140
          | "ALIAS"
141
          | "CAA"
142
          | "CNAME"
143
          | "HTTPS"
144
          | "MX"
145
          | "SRV"
146
          | "TXT"
147
          | "NS";
148
      })
149
    | ({
150
        name: string;
151
        type: "TXT";
152
        ttl?: number;
153
        value: string;
154
        comment?: string;
155
      } & {
156
        type:
157
          | "A"
158
          | "AAAA"
159
          | "ALIAS"
160
          | "CAA"
161
          | "CNAME"
162
          | "HTTPS"
163
          | "MX"
164
          | "SRV"
165
          | "TXT"
166
          | "NS";
167
      })
168
    | ({
169
        name: string;
170
        type: "NS";
171
        ttl?: number;
172
        value?: string;
173
        comment?: string;
174
      } & {
175
        type:
176
          | "A"
177
          | "AAAA"
178
          | "ALIAS"
179
          | "CAA"
180
          | "CNAME"
181
          | "HTTPS"
182
          | "MX"
183
          | "SRV"
184
          | "TXT"
185
          | "NS";
186
      })
187
    | ({
188
        name: string;
189
        type: "HTTPS";
190
        ttl?: number;
191
        https: { priority: number; target: string; params?: string };
192
        comment?: string;
193
      } & {
194
        type:
195
          | "A"
196
          | "AAAA"
197
          | "ALIAS"
198
          | "CAA"
199
          | "CNAME"
200
          | "HTTPS"
201
          | "MX"
202
          | "SRV"
203
          | "TXT"
204
          | "NS";
205
      }),
206
) {
207
  const url = new URL(`https://api.vercel.com/v2/domains/${domain}/records`);
208
  for (const [k, v] of [
209
    ["teamId", teamId],
210
    ["slug", slug],
211
  ]) {
212
    if (v !== undefined && v !== "" && k !== undefined) {
213
      url.searchParams.append(k, v);
214
    }
215
  }
216
  const response = await fetch(url, {
217
    method: "POST",
218
    headers: {
219
      "Content-Type": "application/json",
220
      Authorization: "Bearer " + auth.token,
221
    },
222
    body: JSON.stringify(body),
223
  });
224
  if (!response.ok) {
225
    const text = await response.text();
226
    throw new Error(`${response.status} ${text}`);
227
  }
228
  return await response.json();
229
}
230