← All languages

Servirtium for

F#

No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.

FFI mechanism
Servirtium.Vcr (C#) assembly, no second FFI. F# consumes the shared Servirtium.Vcr C# assembly (P/Invoke over the aether_vcr_embed_* C ABI); the one source file Servirtium.fs adds loan-pattern combinators and curry-friendly members.
Toolchain
.NET 8 SDK or newer (the repo's F# projects target net8.0 with RollForward LatestMajor; verified with the .NET 10 SDK)
Distribution
Uses the release Servirtium.Vcr.2.0.0-alpha.20.nupkg (no F#-specific package exists); copy the one file Servirtium.fs for the combinators
Finding the library
Same as .NET: deployed from the Servirtium.Vcr package's runtimes/<rid>/native/; SERVIRTIUM_VCR_LIB overrides (only FreeBSD needs it).
On GitHub
fsharp/ · README

Getting it

There is no published package for F#. You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.

  1. Put the release Servirtium.Vcr package in a local NuGet feed and add it to your F# project. There is no Servirtium.Vcr.FSharp package on any feed or release page; do not add that reference.
    set -eu
    VER=2.0.0-alpha.20
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/v2.0.0-alpha.20
    mkdir -p nupkgs
    curl -fsSL -o nupkgs/Servirtium.Vcr.$VER.nupkg $REL/Servirtium.Vcr.$VER.nupkg
    curl -fsSL -o nupkgs/Servirtium.Vcr.$VER.nupkg.sha256 $REL/Servirtium.Vcr.$VER.nupkg.sha256
    ( cd nupkgs && { sha256sum -c Servirtium.Vcr.$VER.nupkg.sha256 2>/dev/null || shasum -a 256 -c Servirtium.Vcr.$VER.nupkg.sha256; } )
    [ -f nuget.config ] || dotnet new nugetconfig
    dotnet nuget add source "$PWD/nupkgs" --name servirtium-local --configfile nuget.config
    dotnet add package Servirtium.Vcr --version $VER
  2. Optional: the F# combinators are one source file. Copy it in and compile it before the files that use it.
    curl -fsSL -o Servirtium.fs https://raw.githubusercontent.com/servirtium/servirtium-vcr/v2.0.0-alpha.20/fsharp/Servirtium.Vcr.FSharp/Servirtium.fs
    # in the .fsproj, above Program.fs:
    #   <Compile Include="Servirtium.fs" />
  3. Check it: replay a recorded tape.
    mkdir -p tapes
    curl -fsSL -o tapes/single_get.md https://raw.githubusercontent.com/servirtium/servirtium-vcr/v2.0.0-alpha.20/fsharp/tapes/single_get.md
    cat > Program.fs <<'FS'
    open System.Net.Http
    open Servirtium.Vcr
    [<EntryPoint>]
    let main _ =
        use vcr = Vcr.Playback("tapes/single_get.md").Port(0).Start()
        use http = new HttpClient()
        printfn "%s %A" (http.GetStringAsync(vcr.BaseUrl + "/ok").Result.Trim()) vcr.LastKind   // -> ok-body Ok
        0
    FS
    dotnet run

Record

Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.

open System.Net.Http
open Servirtium.Vcr
open Servirtium.Vcr.FSharp

// usingRecord disposes on every path and discards the recording if the body throws.
// The body must call Commit(): a recording left uncommitted writes nothing and throws.
Servirtium.usingRecord "https://climatedataapi.worldbank.org" "tapes/climate_api.md" (fun vcr ->
    use client = new HttpClient()
    client.GetStringAsync(Servirtium.baseUrl vcr + "/api/v1/countries").Result |> ignore
    vcr.Commit())

Playback

The same tape, replayed with no network.

open System.Net.Http
open Servirtium.Vcr
open Servirtium.Vcr.FSharp

Servirtium.usingPlayback "tapes/single_get.md" (fun vcr ->
    use client = new HttpClient()
    let body = client.GetStringAsync(Servirtium.baseUrl vcr + "/ok").Result
    assert (body = "ok-body")
    assert (Servirtium.matchedCleanly vcr))     // clean match

Idiomatic extras

Builder steps as pipeline functions

Presets, redactions and the opt-in stable order pipe over a RecordBuilder; the JSON rules need prettyJson, else committing throws VcrException.

Vcr.Record(tape, upstream)
|> Servirtium.removeTransportHeaders
|> Servirtium.redactCredentials
|> Servirtium.prettyJson 2
|> Servirtium.sortJsonKeys
|> Servirtium.sortJsonArray "versions" "/created"
|> Servirtium.sortResponseHeaders
|> fun b -> b.Start()

Replay only a slice of a tape

Interaction numbers are 0-based and inclusive; to < 0 means through the last.

Vcr.Playback(tape) |> Servirtium.playbackRange 4 6 |> fun b -> b.Start()

Gotchas

  • Everything in the .NET notes applies (NU1101/NU1102, 64-bit only, no SERVIRTIUM_VCR_LIB unless FreeBSD).
  • Compile order matters in F#: Servirtium.fs must come before any file that uses Servirtium.usingPlayback.
  • Do not ProjectReference the repo's fsharp/ or dotnet/ projects: they build only through aeb. The repo's own fsharp/example references Servirtium.Vcr.FSharp from a feed that aeb builds locally; that package is not published.
  • usingRecord discards the recording if the body throws; if the body returns without Commit() or Discard() it writes nothing and throws.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your F# project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.

Hey Gemini or Claude or Codex,

I want a test in this F# project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.

1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.

2. Add the Servirtium binding for F#. Follow https://servirtium.dev/languages/fsharp/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: Uses the release Servirtium.Vcr.2.0.0-alpha.20.nupkg (no F#-specific package exists); copy the one file Servirtium.fs for the combinators.
   Toolchain and minimum version: .NET 8 SDK or newer (the repo's F# projects target net8.0 with RollForward LatestMajor; verified with the .NET 10 SDK).
   How the binding finds the native library: Same as .NET: deployed from the Servirtium.Vcr package's runtimes/<rid>/native/; SERVIRTIUM_VCR_LIB overrides (only FreeBSD needs it).
   This is a thin layer over the .NET binding: install the Servirtium.Vcr package too, as the page says.

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic F# way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.

4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for F# is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.

5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.

6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.

7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.

Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- Everything in the .NET notes applies (NU1101/NU1102, 64-bit only, no SERVIRTIUM_VCR_LIB unless FreeBSD).
- Compile order matters in F#: Servirtium.fs must come before any file that uses Servirtium.usingPlayback.
- Do not ProjectReference the repo's fsharp/ or dotnet/ projects: they build only through aeb. The repo's own fsharp/example references Servirtium.Vcr.FSharp from a feed that aeb builds locally; that package is not published.
- usingRecord discards the recording if the body throws; if the body returns without Commit() or Discard() it writes nothing and throws.
- A 32-bit process is not supported: there is no 32-bit native engine. Use x64 or ARM64 (on .NET Framework 4.8, set PlatformTarget to x64 and Prefer32Bit to false).

References:
- This language's page: https://servirtium.dev/languages/fsharp/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/fsharp
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)

How these snippets were checked

On linux x64 (cachyos, .NET SDK 10.0.114): fresh F# console project using the release Servirtium.Vcr.2.0.0-alpha.20.nupkg plus the tag's Servirtium.fs copied in; ran the playback snippet (ok-body, matchedCleanly true) and the usingRecord snippet with Commit() against a local HttpListener upstream (tape written, 401 bytes). The restore resolved from the NuGet cache already populated from the local feed by the earlier .NET project, so the feed setup itself was proved by the .NET entry, not re-proved here. The pipeline-builder snippet mirrors fsharp/README.md and the Servirtium.fs signatures; not run.

They mirror these files in the repository:

See also the feature reference, llms.txt and the fsharp/ directory.