0

Add a domain to a project

by
Published Apr 8, 2025

Add a domain to the project by passing its domain name and by specifying the project by either passing the project `id` or `name` in the URL. If the domain is not yet verified to be used on this project, the request will return `verified = false`, and the domain will need to be verified according to the `verification` challenge via `POST /projects/:idOrName/domains/:domain/verify`. If the domain already exists on the project, the request will fail with a `400` status code.

Script vercel Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Vercel = {
3
  token: string;
4
};
5
/**
6
 * Add a domain to a project
7
 * Add a domain to the project by passing its domain name and by specifying the project by either passing the project `id` or `name` in the URL. If the domain is not yet verified to be used on this project, the request will return `verified = false`, and the domain will need to be verified according to the `verification` challenge via `POST /projects/:idOrName/domains/:domain/verify`. If the domain already exists on the project, the request will fail with a `400` status code.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  idOrName: string,
12
  teamId: string | undefined,
13
  slug: string | undefined,
14
  body: {
15
    name: string;
16
    gitBranch?: string;
17
    customEnvironmentId?: string;
18
    redirect?: string;
19
    redirectStatusCode?: 301 | 302 | 307 | 308;
20
  },
21
) {
22
  const url = new URL(
23
    `https://api.vercel.com/v10/projects/${idOrName}/domains`,
24
  );
25
  for (const [k, v] of [
26
    ["teamId", teamId],
27
    ["slug", slug],
28
  ]) {
29
    if (v !== undefined && v !== "" && k !== undefined) {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "POST",
35
    headers: {
36
      "Content-Type": "application/json",
37
      Authorization: "Bearer " + auth.token,
38
    },
39
    body: JSON.stringify(body),
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47