Servirtium for
Elixir
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
- shared C NIF (servirtium_nif OTP app). Servirtium.Native delegates to the :servirtium_nif module, one NIF shared with the Erlang, Gleam and LFE bindings. The NIF only drives libservirtium_vcr's control surface; the library is the HTTP server the SUT talks to over plain HTTP. The Elixir package compiles no C of its own.
- Toolchain
- Elixir ~> 1.15 (mix.exs), Erlang/OTP with erl, erlc, cc and curl to build the NIF app. Checked on Elixir 1.20.4 / OTP 29.
- Distribution
- Not on Hex. Mix git dependency (sparse: elixir) plus a NIF app built from the release library by erlang/build-nif-app.sh
- Finding the library
- The shared servirtium_nif app carries priv/servirtium_nif.so and libservirtium_vcr beside it; the NIF finds the library through a $ORIGIN rpath (@loader_path on macOS), so move the app directory whole. mix ignores ERL_LIBS, so test_helper.exs puts servirtium_nif/ebin on the code path with Code.append_path/1. Do not set SERVIRTIUM_VCR_LIB.
- On GitHub
- elixir/ · README
Getting it
There is no published package for Elixir. 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.
- Build the native OTP apps from the release library (no Aether needed; needs erl, erlc, cc, curl). The script downloads and sha256-checks the asset for your platform, compiles the NIF, and builds servirtium_nif (and servirtium_erl, which Elixir does not need) into ./beam-libs.
VER=v2.0.0-alpha.20 RAW=https://raw.githubusercontent.com/servirtium/servirtium-vcr mkdir -p beam-libs curl -fsSL $RAW/$VER/erlang/build-nif-app.sh | SRC=$VER VER=$VER sh -s -- ./beam-libs - Add the dependency to mix.exs: the repo's elixir/ subdirectory from GitHub at the tag (sparse checkout), then fetch. A local checkout also works: {:servirtium, path: "/path/to/servirtium-vcr/elixir"}.
# mix.exs, in deps: {:servirtium, github: "servirtium/servirtium-vcr", tag: "v2.0.0-alpha.20", sparse: "elixir", only: :test} mix deps.get - mix does not read ERL_LIBS: put the NIF app on the code path in test/test_helper.exs. The path must be .../servirtium_nif/ebin, not beam-libs.
# test/test_helper.exs Code.append_path(System.get_env("SERVIRTIUM_NIF_EBIN") || "beam-libs/servirtium_nif/ebin") ExUnit.start() - Check it: the repo's consumer test replays a canned tape.
mkdir -p tapes curl -fsSL -o tapes/single_get.md $RAW/$VER/elixir/example/tapes/single_get.md curl -fsSL -o test/consumer_test.exs $RAW/$VER/elixir/example/test/consumer_test.exs SERVIRTIUM_NIF_EBIN=$PWD/beam-libs/servirtium_nif/ebin mix test
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.
Servirtium.with_record("tapes/climate_api.md",
"https://climatedataapi.worldbank.org", [port: 0], fn srv ->
:httpc.request(:get, {~c"#{Servirtium.base_url(srv)}/api/v1/countries", []}, [], [])
assert Servirtium.last_kind(srv) == :ok
:ok = Servirtium.commit(srv) # writes the tape
end)
# Leaving with_record without commit/1 (or discard/1) writes nothing and raises;
# an exception inside the fn discards the recording.
Playback
The same tape, replayed with no network.
Servirtium.with_playback("tapes/climate_api.md", [port: 0], fn srv ->
{:ok, {{_, 200, _}, _headers, _body}} =
:httpc.request(:get, {~c"#{Servirtium.base_url(srv)}/api/v1/countries", []}, [], [])
assert Servirtium.last_kind(srv) == :ok # optional: clean match
end)
Idiomatic extras
Redact a secret at commit time
Options are a keyword list; a field is :path, :response_body, :request_headers, :request_body or :response_headers.
{:ok, srv} = Servirtium.record(tape, upstream,
redact: [{:response_body, "Bearer abc123", "Bearer REDACTED"}],
redact_credentials: true)
# ... drive the SUT ...
:ok = Servirtium.commit(srv)
Volatile value that round-trips
A {{placeholder}} rule makes a re-record byte-identical; on playback bind() the real value for a response token.
{:ok, srv} = Servirtium.playback(tape,
volatile_rule: [{"response headers", "Date: {{serverDate}}"}],
bind: [{"serverDate", "Tue, 24 Nov 2026 00:00:00 GMT"}])
IO.puts(Servirtium.volatile_report(srv))
Drift detection on commit
With fail_if_changed, commit/1 still writes the tape and returns {:error, msg} when it differs from the committed one.
{:ok, srv} = Servirtium.record(tape, upstream, fail_if_changed: true)
# ... drive the SUT ...
:ok = Servirtium.commit(srv)
Gotchas
- module :servirtium_nif is not available means the NIF app is not on the code path; SERVIRTIUM_NIF_EBIN alone does nothing, the Code.append_path line is what matters.
- The Hex name is servirtium (OTP app :servirtium); the native app is the separate :servirtium_nif. The Erlang wrapper servirtium_erl also defines a module named servirtium: never put both in one project.
- The NIF source and libservirtium_vcr must be the same release: give build-nif-app.sh the same tag for VER (library) and SRC (sources).
- Servirtium.stop/1 on a record that was neither committed nor discarded returns {:error, msg} and writes nothing.
- :httpc sends a few default request headers; with strict_headers: true drop them with remove_header: [{:request_headers, name}].
Prompt for your coding agent
Paste this into Claude, Codex, Gemini or any coding agent, in your Elixir 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 Elixir 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 Elixir. Follow https://servirtium.dev/languages/elixir/ 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 Hex. Mix git dependency (sparse: elixir) plus a NIF app built from the release library by erlang/build-nif-app.sh.
Toolchain and minimum version: Elixir ~> 1.15 (mix.exs), Erlang/OTP with erl, erlc, cc and curl to build the NIF app. Checked on Elixir 1.20.4 / OTP 29..
How the binding finds the native library: The shared servirtium_nif app carries priv/servirtium_nif.so and libservirtium_vcr beside it; the NIF finds the library through a $ORIGIN rpath (@loader_path on macOS), so move the app directory whole. mix ignores ERL_LIBS, so test_helper.exs puts servirtium_nif/ebin on the code path with Code.append_path/1. Do not set SERVIRTIUM_VCR_LIB.
3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Elixir 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 Elixir 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.
- module :servirtium_nif is not available means the NIF app is not on the code path; SERVIRTIUM_NIF_EBIN alone does nothing, the Code.append_path line is what matters.
- The Hex name is servirtium (OTP app :servirtium); the native app is the separate :servirtium_nif. The Erlang wrapper servirtium_erl also defines a module named servirtium: never put both in one project.
- The NIF source and libservirtium_vcr must be the same release: give build-nif-app.sh the same tag for VER (library) and SRC (sources).
- Servirtium.stop/1 on a record that was neither committed nor discarded returns {:error, msg} and writes nothing.
- :httpc sends a few default request headers; with strict_headers: true drop them with remove_header: [{:request_headers, name}].
References:
- This language's page: https://servirtium.dev/languages/elixir/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/elixir
- 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, Elixir 1.20.4, OTP 29): ran the Getting it steps in a fresh mix project (build-nif-app.sh from the tag with SRC=VER, sparse git dep at the tag, test_helper.exs, repo consumer_test.exs): 3 passed. Also ran a test that does with_record + commit against a local upstream, replays the tape with with_playback (last_kind == :ok), and checks a with_record that never commits raises Servirtium.Error and writes no file; redact: and volatile_rule: options accepted and visible in the tape. bind:, redact_credentials: and fail_if_changed: checked against lib/servirtium.ex docs only, not run.
They mirror these files in the repository:
elixir/test/servirtiumelixir/example/test/consumer_test.exselixir/example/mix.exselixir/.example.aeerlang/build-nif-app.sh
See also the feature reference, llms.txt and the elixir/ directory.