Expand description
Application configuration: typed values read by a service, defined in TOML or JSON files beside the code and updated dynamically while the app runs.
use encore::config::{Config, ConfigType, Duration};
use serde::Deserialize;
#[derive(Deserialize, ConfigType)]
struct Checkout {
/// Maximum number of items allowed in a single order.
#[config(min = 1, max = 500)]
max_items: u32,
#[serde(default)]
promo: Option<Promo>,
payment_timeout: Duration,
}
static CHECKOUT: Config<Checkout> = Config::new("checkout");
fn too_many(items: usize) -> bool {
items > CHECKOUT.get().max_items as usize
}Your Rust type defines the schema. Encore extracts it from the source code —
including fields, types, Serde attributes, #[config(...)] constraints, and doc
comments — and validates every value before the service sees it. Validation runs
when you edit a file under encore run, during deployment, and at service startup
(the service refuses to start with an invalid config). Because of this,
Config::get cannot fail.
§Scope
A config’s declaration determines which services can access it, just like secrets:
Config::new("checkout")belongs to the service that declares it. Two services can each declare a config with the same name and hold separate values. It must be declared inside a service.Config::new("checkout").shared()defines a single app-wide value. Any service may read it, and each service that does is recorded as using it.Config::new("checkout").global()defines a single app-wide value available to every service without tracking per-service usage.
A config name has a single scope across the app, just like a secret: a name declared as shared or global cannot also be declared as a service-scoped config.
Both the name and scope must be written directly in the declaration so Encore can parse them statically.
§Values
Config values live in a file named after the config — checkout.config.toml or
checkout.config.json — next to the declaring service’s encore.service.json
(or next to encore.app.json for shared or global configs). Environment-specific
overlay files are merged on top field by field using JSON Merge Patch (tables
merge, other values replace, and null deletes):
checkout.config.toml every environment
checkout.production.config.toml environments of a type: local, test,
development, preview, production
checkout.@prod-eu.config.toml the environment named prod-eu
checkout.user.config.toml your local overrides, applied on `encore run` onlyKeep user override files out of version control; they apply only to your local
encore run sessions and never to tests. Any field not set in a config file uses
its code default (#[serde(default)] or Option).
§Snapshots and live updates
Config::get returns a Value, which is a cheap-to-clone snapshot that
dereferences to T. Config values can change while the app is running — editing
a config file under encore run pushes the new value to the service within a
second without restarting. Because each snapshot preserves the value from when it
was created, hold onto one for as long as you need consistency (for example,
across a single request) and call get again for the next. To rebuild derived
objects like clients or connection pools on updates, register a callback with
Config::on_change.
§Secrets
A config field can reference a secret instead of holding a plain value:
#[derive(Deserialize, ConfigType)]
struct Stripe {
endpoint: String,
#[config(secret = "StripeKey")]
api_key: encore::secrets::Secret,
}
let key = CHECKOUT.get().stripe.api_key.value();The referenced secret must share the config’s scope: a service config reads a
service secret, a shared config reads a shared secret, and a global config reads
a global secret. The secret must be declared in code like any other secret
(Secret::new("StripeKey") in the service, or with .shared() / .global()
for app-wide secrets); referencing an undeclared secret is an error. Do not set
secret fields in config files — their values are managed per environment as
secrets.
§Tests
Under encore test, configs load values from the test environment overlay.
Individual tests can override a config value in isolation using
testing::set_config.
Structs§
- Config
- A typed configuration handle declared as a module-level
static:static CHECKOUT: Config<Checkout> = Config::new("checkout");. - Duration
- A duration parsed from human-readable config strings such as
"30s","250ms","1h30m", or"1.5h". - Value
- An immutable snapshot of a config’s value at the time it was read.
Traits§
- Config
Type - Marker trait for types that can be used with
Config.
Derive Macros§
- Config
Type - Derives
ConfigTypefor a config struct or enum, and enables#[config(...)]validation attributes on its fields: