Servirtium for
Erlang
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
- C NIF (servirtium_nif OTP app, owned by this binding). Erlang owns the one NIF for the whole BEAM family: c_src/servirtium_nif.c links libservirtium_vcr and is built once into the servirtium_nif OTP app, which Elixir, Gleam and LFE load too. The Hex package servirtium_erl (module servirtium) is the idiomatic wrapper on top.
- Toolchain
- Erlang/OTP with erl, erlc, cc and curl (no minimum OTP version stated in the repo). Checked on OTP 29; rebar3 is optional.
- Distribution
- Not on Hex. Build servirtium_nif + servirtium_erl from the release library with erlang/build-nif-app.sh
- Finding the library
- priv/ of the servirtium_nif app holds servirtium_nif.so and libservirtium_vcr; the NIF finds the library by a $ORIGIN rpath (@loader_path on macOS), so move the app directory whole. The app is found through ERL_LIBS (code:priv_dir); SERVIRTIUM_NIF_DIR overrides the priv lookup. Do not set SERVIRTIUM_VCR_LIB.
- On GitHub
- erlang/ · README
Getting it
There is no published package for Erlang. 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 both OTP apps into ./beam-libs from the release library: servirtium_nif (NIF + libservirtium_vcr) and servirtium_erl (module servirtium). The script downloads and sha256-checks the asset for your platform. Needs erl, erlc, cc, curl; no Aether.
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 - Put both apps on the code path: erl, escript and rebar3 shells honour ERL_LIBS, and it must name the directory that contains the app directories (beam-libs, not beam-libs/servirtium_nif). Under rebar3 keep ERL_LIBS exported, or copy the two app directories into _checkouts/ and list servirtium_erl in your app's applications.
export ERL_LIBS=$PWD/beam-libs - Check it: replay a canned tape.
mkdir -p tapes curl -fsSL -o tapes/single_get.md $RAW/$VER/erlang/example/tapes/single_get.md curl -fsSL -o consumer.escript $RAW/$VER/erlang/example/consumer.escript escript consumer.escript # -> 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.
Rec = servirtium:record("tapes/climate_api.md", "https://climatedataapi.worldbank.org"),
try
Body = os:cmd("curl -s " ++ servirtium:base_url(Rec) ++ "/api/v1/countries"),
%% ... point the system-under-test at servirtium:base_url(Rec) ...
ok = servirtium:last_kind(Rec),
ok = servirtium:commit(Rec) %% writes the tape; close/1 on an uncommitted recording
catch Class:Reason:Stack -> %% returns {error, Msg} and writes nothing
servirtium:discard(Rec), %% drop the recording on a failed run
erlang:raise(Class, Reason, Stack)
end.
Playback
The same tape, replayed with no network.
Vcr = servirtium:playback("tapes/climate_api.md"), %% OS-assigned port
Body = os:cmd("curl -s " ++ servirtium:base_url(Vcr) ++ "/api/v1/countries"),
ok = servirtium:last_kind(Vcr), %% optional: assert a clean match
ok = servirtium:close(Vcr).
Idiomatic extras
Redact a secret at commit time
Redactions are calls on the running record server, made before the traffic you want scrubbed.
Rec = servirtium:record(Tape, Upstream),
ok = servirtium:redact_header(Rec, "Authorization"),
ok = servirtium:redact_query_param(Rec, "password"),
ok = servirtium:redact_json_field(Rec, "secretKey"),
ok = servirtium:redact_credentials(Rec),
%% ... drive the system under test ...
ok = servirtium:commit(Rec).
Volatile value that round-trips
Options go in before the server starts; a bad rule raises {servirtium, Reason}. A response token must be bound on playback.
Vcr = servirtium:playback_with("tapes/create_case.md", [
{volatile_rule, "response headers", "Date: {{serverDate}}"},
{bind, "serverDate", "Tue, 24 Nov 2026 00:00:00 GMT"}
]),
io:format("~s", [servirtium:volatile_report(Vcr)]),
ok = servirtium:close(Vcr).
Playback range
Replay only interactions 4..6 of a tape (0-based, inclusive); raises {servirtium, Reason} if refused.
Vcr = servirtium:playback("tapes/journey.md"),
ok = servirtium:set_playback_range(Vcr, 4, 6).
Gotchas
- {error,{bad_name,servirtium_nif}} or undef on servirtium_nif:... means ERL_LIBS does not name the directory that contains the app directories, or the shell that runs the code did not inherit it.
- Move the app directory whole: cannot open shared object file from the NIF load means priv/ lost the libservirtium_vcr file that sits beside servirtium_nif.so.
- NIF and libservirtium_vcr must be the same release; the script takes the library from VER and the NIF and Erlang sources from SRC, keep them matched.
- Windows is not covered by the script; FreeBSD and musl builds need the matching release asset (freebsd-x86_64.so, linux-<arch>-musl.so) passed as LIB=/path/to/lib.
- close/1 on a record that was neither committed nor discarded returns {error, Msg} and writes no tape.
Prompt for your coding agent
Paste this into Claude, Codex, Gemini or any coding agent, in your Erlang 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 Erlang 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 Erlang. Follow https://servirtium.dev/languages/erlang/ 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. Build servirtium_nif + servirtium_erl from the release library with erlang/build-nif-app.sh.
Toolchain and minimum version: Erlang/OTP with erl, erlc, cc and curl (no minimum OTP version stated in the repo). Checked on OTP 29; rebar3 is optional..
How the binding finds the native library: priv/ of the servirtium_nif app holds servirtium_nif.so and libservirtium_vcr; the NIF finds the library by a $ORIGIN rpath (@loader_path on macOS), so move the app directory whole. The app is found through ERL_LIBS (code:priv_dir); SERVIRTIUM_NIF_DIR overrides the priv lookup. 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 Erlang 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 Erlang 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.
- {error,{bad_name,servirtium_nif}} or undef on servirtium_nif:... means ERL_LIBS does not name the directory that contains the app directories, or the shell that runs the code did not inherit it.
- Move the app directory whole: cannot open shared object file from the NIF load means priv/ lost the libservirtium_vcr file that sits beside servirtium_nif.so.
- NIF and libservirtium_vcr must be the same release; the script takes the library from VER and the NIF and Erlang sources from SRC, keep them matched.
- Windows is not covered by the script; FreeBSD and musl builds need the matching release asset (freebsd-x86_64.so, linux-<arch>-musl.so) passed as LIB=/path/to/lib.
- close/1 on a record that was neither committed nor discarded returns {error, Msg} and writes no tape.
References:
- This language's page: https://servirtium.dev/languages/erlang/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/erlang
- 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, OTP 29): ran the Getting it steps (build-nif-app.sh from the tag with SRC=VER, ERL_LIBS, repo consumer.escript): PASS[discovery]. Also ran an escript that records /ok from a local upstream, commit/1, replays it (last_kind ok), and shows close/1 without commit returns {error,...} with no tape written; and a record_with volatile_rule + redact_header + redact_credentials run that wrote a tape with no secret in it (the playback then needed the bind option, shown above). playback_range not run here.
They mirror these files in the repository:
erlang/test/playback.escripterlang/example/consumer.escripterlang/build-nif-app.sherlang/.example.ae
See also the feature reference, llms.txt and the erlang/ directory.