0

Edit an environment variable

by
Published Apr 8, 2025

Edit a specific environment variable for a given project by passing the environment variable identifier and either passing 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
 * Edit an environment variable
7
 * Edit a specific environment variable for a given project by passing the environment variable identifier and either passing the project `id` or `name` in the URL.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  idOrName: string,
12
  id: string,
13
  teamId: string | undefined,
14
  slug: string | undefined,
15
  body: {
16
    key?: string;
17
    target?: "production" | "preview" | "development"[];
18
    gitBranch?: string;
19
    type?: "system" | "secret" | "encrypted" | "plain" | "sensitive";
20
    value?: string;
21
    customEnvironmentIds?: string[];
22
    comment?: string;
23
  },
24
) {
25
  const url = new URL(
26
    `https://api.vercel.com/v9/projects/${idOrName}/env/${id}`,
27
  );
28
  for (const [k, v] of [
29
    ["teamId", teamId],
30
    ["slug", slug],
31
  ]) {
32
    if (v !== undefined && v !== "" && k !== undefined) {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "PATCH",
38
    headers: {
39
      "Content-Type": "application/json",
40
      Authorization: "Bearer " + auth.token,
41
    },
42
    body: JSON.stringify(body),
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50