0

Update a custom environment

by
Published Apr 8, 2025

Update a custom environment for the project. Must not be named 'Production' or 'Preview'.

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 custom environment
7
 * Update a custom environment for the project. Must not be named 'Production' or 'Preview'.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  idOrName: string,
12
  environmentSlugOrId: string,
13
  teamId: string | undefined,
14
  slug: string | undefined,
15
  body: {
16
    slug?: string;
17
    description?: string;
18
    branchMatcher?: {
19
      type: "equals" | "startsWith" | "endsWith";
20
      pattern: string;
21
    };
22
  },
23
) {
24
  const url = new URL(
25
    `https://api.vercel.com/v9/projects/${idOrName}/custom-environments/${environmentSlugOrId}`,
26
  );
27
  for (const [k, v] of [
28
    ["teamId", teamId],
29
    ["slug", slug],
30
  ]) {
31
    if (v !== undefined && v !== "" && k !== undefined) {
32
      url.searchParams.append(k, v);
33
    }
34
  }
35
  const response = await fetch(url, {
36
    method: "PATCH",
37
    headers: {
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49