0

Update a project domain

by
Published Apr 8, 2025

Update a project domain's configuration, including the name, git branch and redirect of the 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
 * Update a project domain
7
 * Update a project domain's configuration, including the name, git branch and redirect of the domain.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  idOrName: string,
12
  domain: string,
13
  teamId: string | undefined,
14
  slug: string | undefined,
15
  body: {
16
    gitBranch?: string;
17
    redirect?: string;
18
    redirectStatusCode?: 301 | 302 | 307 | 308;
19
  },
20
) {
21
  const url = new URL(
22
    `https://api.vercel.com/v9/projects/${idOrName}/domains/${domain}`,
23
  );
24
  for (const [k, v] of [
25
    ["teamId", teamId],
26
    ["slug", slug],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "PATCH",
34
    headers: {
35
      "Content-Type": "application/json",
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: JSON.stringify(body),
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46