Repository F# setup
open System
open System.IO
open System.Threading
open System.Threading.Tasks
open Axial
open Axial.Console
open Axial.FileSystem
open Axial.Hosting
open Axial.Hosting.Browser
open Axial.Hosting.Node
open Axial.State
open Axial.Telemetry
open Axial.Telemetry.JavaScript

Layers are not required: when every service can be built synchronously, a record built in code is enough. Once building the environment means awaiting a connection, reading configuration, failing with a typed startup error, or acquiring something that must be released (a database pool, a socket), a layer does that work and releases it in reverse order on shutdown. It is a small set of combinators over Flow and Scope for exactly those needs, and it ships in the core Axial package.

Use a record when you can. Construct the environment directly and hand it to the workflow:

Show page setup
// Setup for the checked examples on this page.
open System
open Axial
open Axial.Console
open Axial
/// Fails the docs test when an example's result differs from the value shown.
let shouldEqual expected actual =
    if actual <> expected then failwithf "Expected %A but got %A" expected actual
Verified
type RegionEnv = { Region: string }

let describeRegion : Flow<RegionEnv, Never, string> =
    flow {
        let! now = Clock.now
        let! region = Flow.envWith _.Region
        return $"{region} at {now.Year}"
    }
Requires & types
Requires: open Axial (packages: Axial)
val describeRegion: Flow<RegionEnv,Never,string>
Verified
let env = { Region = "eu" }
describeRegion |> Flow.withClock (Clock.fromValue (DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero))) |> Flow.run env |> shouldEqual (Exit.Success "eu at 2026")
Requires & types
Requires: open Axial; open System (packages: Axial)
val env: RegionEnv

Direct construction needs no layer. See dependencies.

Use a layer when construction is itself effectful:

  • provisioning can fail, and the failure should be a typed startup error rather than an exception
  • a service must be acquired and released for the duration of the flow Layer.provide runs: acquired during provisioning and released when that flow ends, whether it succeeds, fails, or is interrupted
  • independent parts of the environment should be built in parallel
  • a service needs another service in order to be constructed
Verified
type Connection(name: string, closed: ResizeArray<string>) =
    member _.Name = name
    member _.Close() = closed.Add name

type AppEnv = { Region: string; Connection: Connection }
type AppError = QueryFailed of string

let connectionLayer (closed: ResizeArray<string>) : Layer<unit, Never, Connection> =
    Layer.acquireRelease
        (Layer.succeed (Connection("orders-db", closed)))
        (fun connection ->
            Flow.delay (fun () ->
                connection.Close()
                Flow.ok ()))

let runtime (closed: ResizeArray<string>) : Layer<unit, Never, AppEnv> =
    Layer.merge (Layer.succeed "eu") (connectionLayer closed)
    |> Layer.map (fun (region, connection) -> { Region = region; Connection = connection })

let query : Flow<AppEnv, AppError, string> =
    Flow.envWith (fun env -> env.Connection.Name)

let program (closed: ResizeArray<string>) : Flow<unit, AppError, string> =
    Layer.provide (Layer.widenError (runtime closed)) query
Requires & types
Requires: open Axial (packages: Axial)
val connectionLayer: ResizeArray<string> -> Layer<unit,Never,Connection>
val runtime: ResizeArray<string> -> Layer<unit,Never,AppEnv>
val query: Flow<AppEnv,AppError,string>
val program: ResizeArray<string> -> Flow<unit,AppError,string>

runtime cannot fail, so its error type is Never; Layer.widenError lets it provide a workflow that fails with AppError. The connection is closed when program ends:

Verified
let closed = ResizeArray<string>()
program closed |> Flow.run () |> shouldEqual (Exit.Success "orders-db")
List.ofSeq closed |> shouldEqual [ "orders-db" ]
Requires & types
Requires: open Axial (packages: Axial)
val closed: ResizeArray<string>

Layer.provide is the boundary: it opens a scope, builds the layer inside it, runs the downstream flow with the result, and closes the scope afterwards whether the flow succeeds, fails, or is interrupted.

Two provisioning shapes

Typed startup failure. A layer can validate configuration before constructing application services:

Verified
open Axial.Configuration

let connectionString : Layer<IHasConfig, ConfigError, string> =
    Layer.fromFlow (Config.required "ConnectionString")
Requires & types
Requires: open Axial; open Axial.Configuration (packages: Axial, Axial.Configuration)
val connectionString: Layer<IHasConfig,ConfigError,string>

The error channel is ConfigError, not Never. Layer.provide runs the downstream workflow only after provisioning succeeds, releases resources acquired by earlier steps when a later step fails, and lets independent steps build in parallel with merge. A direct factory returning Result can model the error too; the layer additionally owns acquisition and release.

A service built from another service. An HTTP adapter needs an application-owned client and an explicit clock at its live boundary:

Verified
let httpLayer : Layer<System.Net.Http.HttpClient, Never, Axial.HttpClient.IHttp> =
    Layer.envWith (Axial.HttpClient.Http.live)
Requires & types
Requires: open Axial (packages: Axial, Axial.HttpClient)
val httpLayer: Layer<Net.Http.HttpClient,Never,HttpClient.IHttp>

The layer consumes the client and constructs an explicit HTTP capability. It does not install a runtime clock or log sink. Runtime overrides belong around the flow (Flow.withClock, Flow.withLogSink), not in an environment layer. The same adaptation can be performed directly when a host constructs its record.

An already-built value that cannot fail needs no provisioning. Layer.succeed wraps it:

Verified
Layer.succeed Console.live
Requires & types
Requires: open Axial; open Axial.Console (packages: Axial, Axial.Console)

Console, FileSystem, HttpClient and Process are all of this shape, which is why none of them needs a layer to provide it.

Scopes work without layers

Scopes and acquireRelease do not need a layer. See scopes and resources.

In this section

  1. Layers: construction, composition, and provisioning failure.
  2. Tutorial: the same material worked end to end.