0

Get site env vars

by
Published Oct 17, 2025

Returns all environment variables for a site. This convenience method behaves the same as `getEnvVars` but doesn't require an `account_id` as input.

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 site env vars
7
 * Returns all environment variables for a site. This convenience method behaves the same as `getEnvVars` but doesn't require an `account_id` as input.
8
 */
9
export async function main(
10
  auth: Netlify,
11
  site_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
) {
21
  const url = new URL(
22
    `https://api.netlify.com/api/v1/api/v1/sites/${site_id}/env`,
23
  );
24
  for (const [k, v] of [
25
    ["context_name", context_name],
26
    ["scope", scope],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45