| name | env |
|---|---|
| description | Typed access to environment variables, with platform-specific implementations for Node and browser. |
| context | common |
Typed access to environment variables. The Env abstraction lives in @webiny/stdlib (common) and has platform-specific implementations: ProcessEnv (Node, backed by process.env) and BrowserEnv (browser, backed by an injected variables object).
getString(key)/getString(key, default)— read a string variablegetStringOrThrow(key)— read a string or throw if not setgetNumber(key)/getNumber(key, default)— parse a numeric variable; returnsundefined(or default) for missing or unparseable valuesgetNumberOrThrow(key)— parse a number or throw if not set or not a valid numbergetBoolean(key)/getBoolean(key, default)— parse a boolean variable usingtoBoolean; returnsundefined(or default) if not setgetBooleanOrThrow(key)— parse a boolean or throw if not set
import { Container } from "@webiny/di";
import { Env } from "@webiny/stdlib";
import { ProcessEnvFeature } from "@webiny/stdlib/node";
const container = new Container();
ProcessEnvFeature.register(container);
const env = container.resolve(Env);
const port = env.getNumber("PORT", 3000);
const debug = env.getBoolean("DEBUG", false);
const secret = env.getStringOrThrow("SECRET_KEY");import { Container } from "@webiny/di";
import { Env } from "@webiny/stdlib";
import { BrowserEnvFeature } from "@webiny/stdlib/browser";
const container = new Container();
BrowserEnvFeature.register(container, {
variables: { API_URL: "https://api.example.com", DEBUG: "true" }
});
const env = container.resolve(Env);
const apiUrl = env.getString("API_URL");