0

Retrieve the environment variables of a project by id or name

by
Published Apr 8, 2025

Retrieve the environment variables for 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 the environment variables of a project by id or name
7
 * Retrieve the environment variables for 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
  gitBranch: string | undefined,
13
  decrypt: "true" | "false" | undefined,
14
  source: string | undefined,
15
  customEnvironmentId: string | undefined,
16
  customEnvironmentSlug: string | undefined,
17
  teamId: string | undefined,
18
  slug: string | undefined,
19
) {
20
  const url = new URL(`https://api.vercel.com/v10/projects/${idOrName}/env`);
21
  for (const [k, v] of [
22
    ["gitBranch", gitBranch],
23
    ["decrypt", decrypt],
24
    ["source", source],
25
    ["customEnvironmentId", customEnvironmentId],
26
    ["customEnvironmentSlug", customEnvironmentSlug],
27
    ["teamId", teamId],
28
    ["slug", slug],
29
  ]) {
30
    if (v !== undefined && v !== "" && k !== undefined) {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      Authorization: "Bearer " + auth.token,
38
    },
39
    body: undefined,
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