Skip to main content
Version: 2.x

Getting Started

This page takes you from an empty sbt project to a running program that reads its settings from a HOCON file. You add three dependencies, describe a configuration with a case class, load it, and run it.

Terms Used on This Pageโ€‹

  • Config description: a value of type Config[A] that says which keys to read and how to turn them into an A. It does not read anything by itself.
  • Config provider: a value of type ConfigProvider that holds the raw key-value data. It is built from a source such as a HOCON string, a YAML file or a map. You give a config description to a provider to load it.
  • Derivation: generating a config description from a case class automatically, instead of writing it by hand.

1. Add the Dependenciesโ€‹

Add these lines to build.sbt:

// ZIO Config core: config descriptors and readers (JVM)
libraryDependencies += "dev.zio" %% "zio-config" % "4.1.3"

// HOCON and JSON sources
libraryDependencies += "dev.zio" %% "zio-config-typesafe" % "4.1.3"

// automatic derivation
libraryDependencies += "dev.zio" %% "zio-config-magnolia" % "4.1.3"
  • zio-config holds the core API.
  • zio-config-typesafe adds ConfigProvider.fromHoconString and related methods.
  • zio-config-magnolia adds deriveConfig, which builds a config description from a case class.

Other sources (YAML, TOML, XML, AWS Parameter Store) use their own modules. See Config providers.

Some functions used on these pages, such as Config#to and Config#mapKey, come from import zio.config._. Config and ConfigProvider themselves are part of ZIO.

2. Describe the Configurationโ€‹

Define a case class for the settings you need, and derive its config description:

import zio.config.magnolia.deriveConfig

final case class AppConfig(host: String, port: Int)

val appConfigDescription = deriveConfig[AppConfig]

deriveConfig looks at the field names (host, port) and their types. It uses them as the keys to read, so the source must contain host and port.

3. Load It from a Sourceโ€‹

Put the settings in a HOCON string, build a provider from it, and load the description:

import zio._
import zio.config.typesafe._

val settings: String =
"""
host = "localhost"
port = 8080
"""

val provider: ConfigProvider = ConfigProvider.fromHoconString(settings)

val loaded: IO[Config.Error, AppConfig] = provider.load(appConfigDescription)

ConfigProvider#load returns a ZIO. Nothing is read until that effect runs. If a key is missing or has the wrong type, the effect fails with a Config.Error. See Errors.

4. Run Itโ€‹

Wrap the load in a ZIOAppDefault, print the result, and run the program:

import zio._
import zio.config.magnolia.deriveConfig
import zio.config.typesafe._

object Main extends ZIOAppDefault {

final case class AppConfig(host: String, port: Int)

val settings: String =
"""
host = "localhost"
port = 8080
"""

def run =
for {
config <- ConfigProvider.fromHoconString(settings).load(deriveConfig[AppConfig])
_ <- Console.printLine(s"Starting on ${config.host}:${config.port}")
} yield ()
}

Running Main prints Starting on localhost:8080.

Where to Go Nextโ€‹

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–