Servirtium for
Gleam
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
- @external(erlang, ...) onto the shared servirtium_nif C NIF. Gleam compiles to Erlang and runs on the BEAM, so src/servirtium.gleam binds with @external(erlang, "servirtium_nif", ...) to the NIF the Erlang binding builds. Gleam ships no C source and no NIF module; Gleam Strings are Erlang binaries, the term type the NIF takes.
- Toolchain
- Gleam with target = "erlang" (JavaScript target unsupported), gleam_stdlib >= 0.34.0 and < 2.0.0, Erlang/OTP, plus erl, erlc, cc and curl to build the NIF app. Checked on Gleam 1.19.2 / OTP 29.
- Distribution
- Not on Hex. Gleam path dependency onto a checkout of the tag, plus a NIF app built from the release library
- Finding the library
- ERL_LIBS must name the directory containing servirtium_nif (gleam run / gleam test honour it); the NIF finds libservirtium_vcr beside itself in priv/ via a $ORIGIN rpath. Do not set SERVIRTIUM_VCR_LIB.
- On GitHub
- gleam/ · README
Getting it
There is no published package for Gleam. 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.
- Gleam git dependencies cannot point at a subdirectory, and gleam/ lives in a monorepo, so clone the tag and use a path dependency.
VER=v2.0.0-alpha.20 RAW=https://raw.githubusercontent.com/servirtium/servirtium-vcr git clone --depth 1 --branch $VER https://github.com/servirtium/servirtium-vcr.git vendor-src/servirtium-vcr - Build the shared native app from the release library (no Aether; needs erl, erlc, cc, curl).
mkdir -p beam-libs curl -fsSL $RAW/$VER/erlang/build-nif-app.sh | SRC=$VER VER=$VER sh -s -- ./beam-libs - Declare the path dependency in gleam.toml and resolve. Do not edit manifest.toml by hand. (A project must not be named like an OTP module, e.g. gl.)
# gleam.toml [dependencies] servirtium_gleam = { path = "vendor-src/servirtium-vcr/gleam" } gleam deps download - Check it: the repo's consumer program replays a canned tape (ERL_LIBS must be exported in the shell that runs gleam).
mkdir -p tapes curl -fsSL -o tapes/single_get.md $RAW/$VER/gleam/example/tapes/single_get.md curl -fsSL -o src/consumer_check.gleam $RAW/$VER/gleam/example/src/consumer_example.gleam ERL_LIBS=$PWD/beam-libs gleam run -m consumer_check # -> PASS ... rm src/consumer_check.gleam
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
pub fn record_test() {
let rec = servirtium.record("tapes/climate_api.md", "https://climatedataapi.worldbank.org")
// ... point the system-under-test at servirtium.base_url(rec) ...
let _body = servirtium.curl(servirtium.base_url(rec) <> "/api/v1/countries")
let assert servirtium.Ok = servirtium.last_kind(rec)
let assert servirtium.Closed = servirtium.commit(rec) // writes the tape
// servirtium.discard(rec) drops it; close(rec) on an uncommitted recording
// returns CloseError(msg) and writes nothing
}
Playback
The same tape, replayed with no network.
import servirtium
pub fn playback_test() {
let vcr = servirtium.playback("tapes/climate_api.md") // OS-assigned port
let _body = servirtium.curl(servirtium.base_url(vcr) <> "/api/v1/countries")
let assert servirtium.Ok = servirtium.last_kind(vcr) // optional: clean match
let assert servirtium.Closed = servirtium.close(vcr)
}
Idiomatic extras
Redact a secret at commit time
Redactions are calls on the running record handle; each returns Result(Nil, String).
let rec = servirtium.record(tape, upstream)
let assert Ok(_) = servirtium.redact_header(rec, "Authorization")
let assert Ok(_) = servirtium.redact_query_param(rec, "password")
let assert Ok(_) = servirtium.redact_credentials(rec)
// ... drive the system under test ...
let assert servirtium.Closed = servirtium.commit(rec)
Volatile value that round-trips
Options are a list applied before the server starts; a bad rule panics the start. A response token must be bound on playback.
let vcr = servirtium.playback_with("tapes/create_case.md", [
servirtium.VolatileRule("response headers", "Date: {{serverDate}}"),
servirtium.Bind("serverDate", "Tue, 24 Nov 2026 00:00:00 GMT"),
])
let report = servirtium.volatile_report(vcr)
Playback range
Replay only interactions 4..6 of a tape (0-based, inclusive); Error(reason) if refused.
let vcr = servirtium.playback("tapes/journey.md")
let assert Ok(_) = servirtium.set_playback_range(vcr, 4, 6)
Gotchas
- target = "erlang" is required; the JavaScript target is not supported (NIF).
- undef or 'module servirtium_nif is not available' means ERL_LIBS was not exported in the shell running gleam, or it names beam-libs/servirtium_nif instead of beam-libs.
- Keep VER (library) and the checkout tag the same release: the NIF source and the library must match.
- The Outcome type has a variant named Ok that shadows the prelude Ok for this module's own match arms; close/commit/discard therefore return Closed | CloseError(String) rather than Result.
Prompt for your coding agent
Paste this into Claude, Codex, Gemini or any coding agent, in your Gleam 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 Gleam 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 Gleam. Follow https://servirtium.dev/languages/gleam/ 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. Gleam path dependency onto a checkout of the tag, plus a NIF app built from the release library.
Toolchain and minimum version: Gleam with target = "erlang" (JavaScript target unsupported), gleam_stdlib >= 0.34.0 and < 2.0.0, Erlang/OTP, plus erl, erlc, cc and curl to build the NIF app. Checked on Gleam 1.19.2 / OTP 29..
How the binding finds the native library: ERL_LIBS must name the directory containing servirtium_nif (gleam run / gleam test honour it); the NIF finds libservirtium_vcr beside itself in priv/ via a $ORIGIN rpath. 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 Gleam 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 Gleam 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.
- target = "erlang" is required; the JavaScript target is not supported (NIF).
- undef or 'module servirtium_nif is not available' means ERL_LIBS was not exported in the shell running gleam, or it names beam-libs/servirtium_nif instead of beam-libs.
- Keep VER (library) and the checkout tag the same release: the NIF source and the library must match.
- The Outcome type has a variant named Ok that shadows the prelude Ok for this module's own match arms; close/commit/discard therefore return Closed | CloseError(String) rather than Result.
References:
- This language's page: https://servirtium.dev/languages/gleam/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/gleam
- 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, Gleam 1.19.2, OTP 29): ran the Getting it steps in a fresh project (path dep onto a checkout of the tag, build-nif-app.sh with SRC=VER, repo consumer_example.gleam as src/consumer_check.gleam): PASS[discovery]. Also ran a program that records /ok from a local upstream, commit(), replays it (last_kind Ok, close Closed), and shows close() on an uncommitted recording gives CloseError with no tape; and playback_with VolatileRule+Bind plus redact_header/redact_credentials on a record, which ran. Playback range not run.
They mirror these files in the repository:
gleam/test/servirtium_gleam_test.gleamgleam/example/src/consumer_example.gleamgleam/.example.aeerlang/build-nif-app.sh
See also the feature reference, llms.txt and the gleam/ directory.