0

Get env vars

by
Published Oct 17, 2025

Returns all environment variables for an account or site. An account corresponds to a team in the Netlify UI.

Script netlify Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Netlify = {
3
  token: string;
4
};
5
/**
6
 * Get env vars
7
 * Returns all environment variables for an account or site. An account corresponds to a team in the Netlify UI.
8
 */
9
export async function main(
10
  auth: Netlify,
11
  account_id: string,
12
  context_name:
13
    | "all"
14
    | "dev"
15
    | "branch-deploy"
16
    | "deploy-preview"
17
    | "production"
18
    | undefined,
19
  scope: "builds" | "functions" | "runtime" | "post-processing" | undefined,
20
  site_id: string | undefined,
21
) {
22
  const url = new URL(
23
    `https://api.netlify.com/api/v1/accounts/${account_id}/env`,
24
  );
25
  for (const [k, v] of [
26
    ["context_name", context_name],
27
    ["scope", scope],
28
    ["site_id", site_id],
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