Settings: $config
$config holds the app's own settings, such as its name, its database and its cache. Every script, page, route and test can read it without an import. The settings live in one place: the config section of kscript.json.
{
"name": "shop",
"config": {
"app": { "name": "Shop" },
"database": { "connections": { "main": { "url": "sqlite:data/shop.sqlite" } } },
"features": ["search", "cart"]
}
}<echo value="$config.app.name" />
<let name="timeout" value="$config->get('http.timeout', 30)" /> <!-- a path, and a default when it isn't set -->
<if $config->has('features')>
<echo>{ $config.features->join(', ') }</echo>
</if>
<let name="key" value="$config->require('api.key')" /> <!-- stops with a clear message when it's missing -->echo $config.app.name
$timeout = $config->get('http.timeout', 30) // a path, and a default when it isn't set
if $config->has('features') => echo "{ $config.features->join(', ') }"
$key = $config->require('api.key') // stops with a clear message when it's missingA setting that isn't there is null, so $config.port ?? 8080 works. Names don't depend on case, so $config.app.name and $config.APP.NAME are the same setting. In the editor, hovering over $config.app.name shows its type, and Go to Definition opens the key in the file that supplies it, inside interpolated strings too.
Packages read their own parts of $config: KScript.Data reads database, KScript.Cache reads cache, KScript.Mail reads mail, $log reads logging, translations read i18n, and money arithmetic reads money.exchange_rates (see Money).
Environments. The same app often needs other settings in another place: an in-memory database for tests, or a real server in production. Put those in kscript.<env>.json beside kscript.json. Its config section is laid over the one in kscript.json, and maps merge field by field, so it only needs the settings that change:
// kscript.test.json
{
"config": {
"database": { "connections": { "main": { "url": "sqlite::memory:" } } }
}
}Choose the environment with kscript --env staging script.kscript or KSCRIPT_ENV=staging. kscript test uses test.env from kscript.json (see Testing). $config->environment() gives the environment, or null when none is chosen. An environment's file holds only a config section: everything else belongs in kscript.json.
Settings that need code: config.kscript. JSON can't read an environment variable or work something out. For those settings, add config.kscript beside kscript.json (config.ks.xml in an XML project). It defines configure(environment), which gets the environment (or development when none is chosen) and returns a map. That map is laid over the JSON settings, so it only needs the settings that need code:
<!-- config.kscript -->
<fn name="configure" params="environment: string">
<let name="url" value="@env('DATABASE_URL', default: '')" />
<return value="{
database: { connections: { main: $url->is_blank() ? {} : { url: $url } } }
http: { timeout: $environment == 'production' ? 8s : 30s }
}" />
</fn>// config.kscript
fn configure(environment: string) {
$url = @env('DATABASE_URL', default: '')
return {
database: { connections: { main: $url->is_blank() ? {} : { url: $url } } }
http: { timeout: $environment == 'production' ? 8s : 30s }
}
}The values keep their KScript types, so 8s stays a duration. The script can use @env(), $env, imported files and packages such as KScript.IO, and paths in it start from its own folder. It runs once, the first time a script reads $config, and it can't read $config itself. kscript make config base writes a starting config.kscript, and kscript make config staging writes kscript.staging.json.
The layers, later ones winning: the config section of kscript.json, then kscript.<env>.json, then config.kscript. A script outside a project reads config.kscript from its own folder.
.env files keep secrets out of the project's files. .env (and .env.<env> for an environment) beside kscript.json holds NAME=value lines, with # comments, export in front if you like, 'single quotes' taken as written, and "double quotes" that allow \n and can span lines. ${NAME} and ${NAME:-default} use another variable. The values are read with @env('NAME'), usually in config.kscript, and they also become environment variables unless one is already set, so KScript.AI finds ANTHROPIC_API_KEY in .env. Keep .env files out of git.
The person's own settings. A desktop app can let the person change a setting without touching the project: with "app": { "user_config": true } in kscript.json, config.json in the app's config folder is laid over everything else (see App folders).
config reads settings again with other options, or reads another set of settings into a variable. Without into, it changes $config:
<config env="production" />
<config files="themes/dark.json" into="theme" /> <!-- a separate set of settings in $theme -->
<config files="settings.json, secrets.kscript?, .env?" prefix="SHOP_" defaults="{ server: { port: 8080 } }" require="database.host" />config(env: 'production')
config(files: ['themes/dark.json'], into: theme) // a separate set of settings in $theme
config(files: ['settings.json', 'secrets.kscript?', '.env?'], prefix: 'SHOP_', defaults: { server: { port: 8080 } }, require: ['database.host'])files lists other files to read instead of the project's: JSON files of settings, config scripts and .env files, in order, each followed by its version for the environment (settings.staging.json). A name without an extension means its .json file, and a name ending in ? may be missing. With prefix, environment variables override the files and __ nests: SHOP_DATABASE__HOST=db.internal sets database.host, and values such as true, false and 8080 become booleans and numbers. root is the folder the files are in, defaults fills in settings no file sets, require lists settings that must be set, and user: true also reads the person's own settings.
$config->sources() lists the files that were read, $config->all() gives every setting as a map, and $config->reload() reads them again. @parse_env(text) parses .env text, and KScript.IO adds @read_env(path).
kscript serve never serves kscript.json, kscript.<env>.json, scripts, .env files, or config.* JSON files. Pages read the project's settings and can use @env() for values in its .env files, but they leave the process environment alone.