Configuration
env()
Signature
Section titled “Signature”env() — returns any
Available in: controller, model, mapper, migrator, migration, tabledefinition
Category: Miscellaneous Functions
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | yes | — | The environment variable name to look up. |
defaultValue | any | no | — | Value to return if the variable is not found. The legacy |
Examples
Section titled “Examples”// 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.");
}