0

Retrieve project domains by project by id or name

by
Published Apr 8, 2025

Retrieve the domains associated with a given project by passing either the project `id` or `name` in the URL.

Script vercel Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Vercel = {
3
  token: string;
4
};
5
/**
6
 * Retrieve project domains by project by id or name
7
 * Retrieve the domains associated with a given project by passing either the project `id` or `name` in the URL.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  idOrName: string,
12
  production: "true" | "false" | undefined,
13
  target: "production" | "preview" | undefined,
14
  customEnvironmentId: string | undefined,
15
  gitBranch: string | undefined,
16
  redirects: "true" | "false" | undefined,
17
  redirect: string | undefined,
18
  verified: "true" | "false" | undefined,
19
  limit: string | undefined,
20
  since: string | undefined,
21
  until: string | undefined,
22
  order: "ASC" | "DESC" | undefined,
23
  teamId: string | undefined,
24
  slug: string | undefined,
25
) {
26
  const url = new URL(`https://api.vercel.com/v9/projects/${idOrName}/domains`);
27
  for (const [k, v] of [
28
    ["production", production],
29
    ["target", target],
30
    ["customEnvironmentId", customEnvironmentId],
31
    ["gitBranch", gitBranch],
32
    ["redirects", redirects],
33
    ["redirect", redirect],
34
    ["verified", verified],
35
    ["limit", limit],
36
    ["since", since],
37
    ["until", until],
38
    ["order", order],
39
    ["teamId", teamId],
40
    ["slug", slug],
41
  ]) {
42
    if (v !== undefined && v !== "" && k !== undefined) {
43
      url.searchParams.append(k, v);
44
    }
45
  }
46
  const response = await fetch(url, {
47
    method: "GET",
48
    headers: {
49
      Authorization: "Bearer " + auth.token,
50
    },
51
    body: undefined,
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59