← All languages

Servirtium for

Haskell

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
foreign import ccall (link-time). The servirtium package declares the C ABI with foreign import ccall and links libservirtium_vcr (extra-libraries: servirtium_vcr). Options are plain records tweaked with record-update syntax; withPlayback / withRecord are bracket-style.
Toolchain
GHC with base >= 4.11 (GHC 8.4+) and cabal (cabal-version 1.12 package, cabal-install 3.x project files). Checked on GHC 9.6.7 and cabal-install 3.10.3.
Distribution
Not on Hackage. Cabal project against a checkout of the tag, plus the release native library
Finding the library
Link time: cabal.project.local gives extra-lib-dirs (absolute path) to a directory holding libservirtium_vcr.so/.dylib, plus an -rpath so the built binary finds it. SERVIRTIUM_VCR_LIB is for the repo's own tests, not consumers.
On GitHub
haskell/ · README

Getting it

There is no published package for Haskell. 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. Clone the tag (only haskell/ is used). The README's source-repository-package stanza (type: git, subdir: haskell) did not build with cabal 3.10.3: the haskell/ directory has no LICENSE file but servirtium.cabal says license-file: LICENSE, so cabal failed with 'LICENSE: withBinaryFile: does not exist'. A local checkout with packages: works.
    VER=v2.0.0-alpha.20
    git clone --depth 1 --branch $VER https://github.com/servirtium/servirtium-vcr.git vendor-src/servirtium-vcr
    
  2. Download the native library for your platform into ./native, verify it, and copy it to the plain name the linker wants (libservirtium_vcr.so, or .dylib on macOS).
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/$VER
    # Linux x86_64 shown; macOS arm64 is macos-arm64 + .dylib
    LIB=libservirtium_vcr-$VER-linux-x86_64.so
    mkdir -p native
    curl -fsSL -o native/$LIB $REL/$LIB
    curl -fsSL -o native/$LIB.sha256 $REL/$LIB.sha256
    ( cd native && { sha256sum -c $LIB.sha256 || shasum -a 256 -c $LIB.sha256; } )
    cp native/$LIB native/libservirtium_vcr.so
    
  3. cabal.project names your package and the checkout's haskell/ directory. cabal.project.local is mandatory (without it configure fails with 'Missing (or bad) C library: servirtium_vcr'); extra-lib-dirs must be an absolute path. Do not commit cabal.project.local.
    cat > cabal.project <<EOF
    packages: .
              vendor-src/servirtium-vcr/haskell
    EOF
    cat > cabal.project.local <<EOF
    package servirtium
      extra-lib-dirs: $PWD/native
      ghc-options: -optl-Wl,-rpath,$PWD/native
    EOF
    
  4. In your own .cabal it is simply build-depends: base, servirtium. Check it with the repo's consumer example (needs curl on PATH).
    RAW=https://raw.githubusercontent.com/servirtium/servirtium-vcr/$VER/haskell/example
    curl -fsSL -O $RAW/Main.hs -O $RAW/consumer-example.cabal -O $RAW/single_get.md
    cabal run consumer-example      # -> PASS[discovery]: ...
    

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.

import Servirtium.Vcr

main :: IO ()
main =
  withRecord (recordOptions "tapes/climate_api.md" "https://climatedataapi.worldbank.org") $ \vcr -> do
    url <- baseUrl vcr
    -- ... drive the SUT against `url` ...
    k <- lastKind vcr
    if k /= Ok then lastError vcr >>= error else pure ()
    commit vcr     -- writes the tape. Returning without commit or discard throws VcrException
                   -- and writes nothing; an exception in the body discards the recording.

Playback

The same tape, replayed with no network.

import Servirtium.Vcr

main :: IO ()
main =
  withPlayback (playbackOptions "tapes/climate_api.md") $ \vcr -> do   -- pbPort = 0 (default): OS-assigned
    url <- baseUrl vcr
    -- ... drive the SUT against `url` ...
    k <- lastKind vcr
    k `shouldBe` Ok       -- hspec's shouldBe, or any check of your own

Idiomatic extras

Redact a secret at commit time

Record options are fields tweaked with record-update syntax; Field is Path, ResponseBody, RequestHeaders, RequestBody or ResponseHeaders.

withRecord (recordOptions tape upstream)
  { recRedactions        = [(ResponseBody, "Bearer abc123", "Bearer REDACTED")]
  , recRedactCredentials = True } $ \vcr -> do
    -- ... drive the SUT ...
    commit vcr

Volatile value that round-trips

A {{placeholder}} rule makes a re-record byte-identical; on playback bind the real value for a response token.

withPlayback (playbackOptions tape)
  { pbVolatileRules = [volatileRule "response headers" "Date: {{serverDate}}"]
  , pbVolatileBinds = [("serverDate", "Tue, 24 Nov 2026 00:00:00 GMT")] } $ \vcr ->
    volatileReport vcr

Drift detection

With recFailIfChanged, commit writes the new tape and throws VcrException when it differs from the committed one.

withRecord (recordOptions tape upstream) { recFailIfChanged = True } $ \vcr -> do
  -- ... drive the SUT ...
  commit vcr

Gotchas

  • cabal.project.local is mandatory and must hold an absolute extra-lib-dirs; do not apt/brew install anything for 'Missing (or bad) C library: servirtium_vcr'.
  • Without the rpath, run the binary with LD_LIBRARY_PATH=$PWD/native (DYLD_LIBRARY_PATH on macOS).
  • A dynamic-only GHC (Arch) cannot build the default static way ('files missing in base'): add library-vanilla: False, shared: True and executable-dynamic: True to cabal.project.local (as haskell/.example.ae does).
  • The package version is 2.0.0 (PVP versions cannot spell alpha); it follows the release line v2.0.0-alpha.N.
  • Haskell HTTP clients send default request headers (Host, Accept-Encoding); under pbStrictHeaders remove them with recRemoveHeaders/pbRemoveHeaders.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your Haskell 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 Haskell 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 Haskell. Follow https://servirtium.dev/languages/haskell/ 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: Not on Hackage. Cabal project against a checkout of the tag, plus the release native library.
   Toolchain and minimum version: GHC with base >= 4.11 (GHC 8.4+) and cabal (cabal-version 1.12 package, cabal-install 3.x project files). Checked on GHC 9.6.7 and cabal-install 3.10.3..
   How the binding finds the native library: Link time: cabal.project.local gives extra-lib-dirs (absolute path) to a directory holding libservirtium_vcr.so/.dylib, plus an -rpath so the built binary finds it. SERVIRTIUM_VCR_LIB is for the repo's own tests, not consumers.

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Haskell 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 Haskell 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.
- cabal.project.local is mandatory and must hold an absolute extra-lib-dirs; do not apt/brew install anything for 'Missing (or bad) C library: servirtium_vcr'.
- Without the rpath, run the binary with LD_LIBRARY_PATH=$PWD/native (DYLD_LIBRARY_PATH on macOS).
- A dynamic-only GHC (Arch) cannot build the default static way ('files missing in base'): add library-vanilla: False, shared: True and executable-dynamic: True to cabal.project.local (as haskell/.example.ae does).
- The package version is 2.0.0 (PVP versions cannot spell alpha); it follows the release line v2.0.0-alpha.N.
- Haskell HTTP clients send default request headers (Host, Accept-Encoding); under pbStrictHeaders remove them with recRemoveHeaders/pbRemoveHeaders.

References:
- This language's page: https://servirtium.dev/languages/haskell/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/haskell
- 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 cachyos (linux-x86_64, GHC 9.6.7, cabal 3.10.3): downloaded the release .so with sha256 check, built the repo's consumer example against a checkout of the tag with the cabal.project.local above (plus the dynamic-only GHC flags) and got PASS[discovery]. The README's source-repository-package route failed on the missing haskell/LICENSE (see doc-problems). Also ran a withRecord/commit program with recRedactions against a local upstream: tape written, replayed Ok with the secret scrubbed; withRecord with neither commit nor discard threw VcrException and wrote no file. recRedactCredentials, pbVolatileRules/volatileRule, recFailIfChanged checked against src/Servirtium/Vcr.hs only, not run.

They mirror these files in the repository:

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