Skip to content

Configuration

env()

env() — returns any

Available in: controller, model, mapper, migrator, migration, tabledefinition Category: Miscellaneous Functions

Returns the value of an environment variable. Checks application.env (loaded from .env files) first, then falls back to system environment variables (server.system.environment). Returns the default if the variable is not found in either location.

named argument default is also accepted for backwards compatibility with pre-rename callers.

NameTypeRequiredDefaultDescription
namestringyes—The environment variable name to look up.
defaultValueanyno—Value to return if the variable is not found. The legacy
// 1. Read a required environment variable
dbUrl = env("DATABASE_URL");
// dbUrl -> "postgres://user:pass@localhost/myapp" (or "" if not set)

// 2. Provide a default when the variable may be absent
smtpHost = env("SMTP_HOST", "localhost");
// smtpHost -> "localhost" when SMTP_HOST is not defined in .env or system env

// 3. Guard application startup based on an environment variable
appSecret = env("APP_SECRET_KEY");
if (!Len(appSecret)) {
	throw(type="App.ConfigError", message="APP_SECRET_KEY must be set.");
}