← All languages

Servirtium for

C

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
direct link (no FFI). The library's ABI is already C, so the binding is one header (servirtium.h) and one source file (servirtium.c) that you compile with your own code; the native library is linked at build time, not dlopen-ed. The header adds enums, owned-string handling (sv_str / sv_free) and open-plus-start-plus-error in one call per mode.
Toolchain
A C99 compiler (cc/gcc/clang); POSIX popen needs -std=gnu99 only for the check program. No Aether toolchain.
Distribution
Vendor servirtium.h + servirtium.c at the tag, plus the native library from the release
Finding the library
Linked at build time: -L<dir> -lservirtium_vcr with an rpath to <dir>. <dir> must hold a file named exactly libservirtium_vcr.so / .dylib (the release asset name carries tag/os/arch, so copy it to the plain name). On macOS the release dylib carries a CI install name, so rewrite it (see the install steps).
On GitHub
c/ · README

Getting it

There is no published package for C. 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. Fetch the native library for your OS/arch from the release, verify its checksum, and copy it to the plain name the linker wants (this is the README's step 1, collapsed; Linux x86_64 shown).
    TAG=v2.0.0-alpha.20
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/$TAG
    LIBDIR="$HOME/.servirtium-vcr/$TAG"; mkdir -p "$LIBDIR"
    LIB=libservirtium_vcr-$TAG-linux-x86_64.so      # macos-arm64.dylib, linux-arm64-musl.so, freebsd-x86_64.so, ...
    curl -fsSL -o "$LIBDIR/$LIB" "$REL/$LIB"
    curl -fsSL -o "$LIBDIR/$LIB.sha256" "$REL/$LIB.sha256"
    ( cd "$LIBDIR" && sha256sum -c "$LIB.sha256" )   # macOS: shasum -a 256 -c
    cp "$LIBDIR/$LIB" "$LIBDIR/libservirtium_vcr.so"
    
  2. macOS only: the release .dylib has a Linux CI path as its install name, so a program linked against it fails at run time with 'Library not loaded: /home/runner/...'. Give your copy an rpath-relative install name (and re-sign it on Apple silicon) before linking.
    install_name_tool -id @rpath/libservirtium_vcr.dylib "$LIBDIR/libservirtium_vcr.dylib"
    codesign -f -s - "$LIBDIR/libservirtium_vcr.dylib"
    
  3. Vendor the two binding files from the same tag (keep them at one tag with the library).
    RAW=https://raw.githubusercontent.com/servirtium/servirtium-vcr/$TAG
    mkdir -p third_party/servirtium
    curl -fsSL -o third_party/servirtium/servirtium.h $RAW/c/include/servirtium.h
    curl -fsSL -o third_party/servirtium/servirtium.c $RAW/c/src/servirtium.c
    
  4. Compile servirtium.c as C together with your sources and link the library, with an rpath so the binary finds it without LD_LIBRARY_PATH.
    cc -std=c99 -Ithird_party/servirtium yours.c third_party/servirtium/servirtium.c \
       -L"$LIBDIR" -lservirtium_vcr -Wl,-rpath,"$LIBDIR" -o yours
    

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.

#include <servirtium.h>
#include <stdio.h>

int main(void) {
    sv_str err, e;
    sv_vcr *rec = sv_record("tapes/climate_api.md", "https://example.org", &err);
    if (rec == NULL) { fprintf(stderr, "%s\n", err.ptr); sv_free(err); return 1; }

    sv_str base = sv_base_url(rec);
    /* ... point the system-under-test at base.ptr and drive it ... */
    sv_free(base);

    /* Commit: writes the tape. "" means OK, else the reason. */
    e = sv_close_and_flush(rec, NULL);
    if (e.len) { fprintf(stderr, "flush: %s\n", e.ptr); sv_free(e); return 1; }
    sv_free(e);
    return 0;
}

Playback

The same tape, replayed with no network.

#include <servirtium.h>
#include <assert.h>
#include <stdio.h>

int main(void) {
    sv_str err;
    sv_vcr *vcr = sv_playback("tapes/single_get.md", &err);   /* error may be NULL */
    if (vcr == NULL) { fprintf(stderr, "%s\n", err.ptr); sv_free(err); return 1; }

    sv_str base = sv_base_url(vcr);        /* "http://127.0.0.1:54213" */
    /* ... point the system-under-test at base.ptr and drive it ... */
    assert(sv_last_kind(vcr) == SV_OK);    /* optional: assert a clean match */

    sv_free(base);
    sv_close(vcr);
    return 0;
}

Idiomatic extras

Drop a recording instead of writing it

On a failed test call sv_discard so a half-finished recording never becomes a tape. sv_close on a recording that was neither flushed nor discarded writes nothing and returns an error string saying so.

sv_vcr *rec = sv_record("tapes/api.md", upstream, &err);
/* ... drive the system-under-test ... */
if (!test_passed) {
    sv_discard(rec, NULL);                   /* nothing written */
} else {
    sv_str e = sv_close_and_flush(rec, NULL);
    sv_free(e);
}

Redact secrets before the tape is written

Each config call returns an owned string: empty on success, else the core's reason. Register the same rules on the playback handle too.

sv_str e;
e = sv_redact_credentials(rec);            sv_free(e);   /* Authorization, Cookie, X-Api-Key, ... */
e = sv_redact_credential_header(rec, "X-Tenant-Token"); sv_free(e);
e = sv_redact_header(rec, "X-Request-Signature");       sv_free(e);

Volatile values and a rewritten upstream base

Name a value that changes every run with a {{placeholder}}; the tape holds a token and playback matches any live value. rewrite_upstream_base keeps the upstream's own absolute URLs pointing at the recorder.

sv_str e = sv_volatile_rule(rec, "request body",
    "<a:MessageID>urn:uuid:{{messageId}}</a:MessageID>");
sv_free(e);
sv_set_rewrite_upstream_base(rec, 1);

Gotchas

  • No package: vendor servirtium.h and servirtium.c at the tag and link the downloaded native library.
  • Compile servirtium.c as C, never with a C++ compiler (the extern symbols would be mangled).
  • -lservirtium_vcr needs the plain file name libservirtium_vcr.so/.dylib; the versioned release asset name does not satisfy it.
  • The library must be at least as new as servirtium.c: use the same tag for both, or the link fails with undefined reference to aether_vcr_embed_...
  • A recording closed with neither sv_close_and_flush nor sv_discard writes nothing and sv_close returns an error.
  • macOS: the release dylib's install name is a Linux CI path; run install_name_tool -id @rpath/... (and codesign -f -s -) on your copy before linking.
  • Windows: take the .dll asset with its .dll.lib import library by hand.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your C 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 C 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 C. Follow https://servirtium.dev/languages/c/ 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: Vendor servirtium.h + servirtium.c at the tag, plus the native library from the release.
   Toolchain and minimum version: A C99 compiler (cc/gcc/clang); POSIX popen needs -std=gnu99 only for the check program. No Aether toolchain..
   How the binding finds the native library: Linked at build time: -L<dir> -lservirtium_vcr with an rpath to <dir>. <dir> must hold a file named exactly libservirtium_vcr.so / .dylib (the release asset name carries tag/os/arch, so copy it to the plain name). On macOS the release dylib carries a CI install name, so rewrite it (see the install steps).

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic C 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 C 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.
- No package: vendor servirtium.h and servirtium.c at the tag and link the downloaded native library.
- Compile servirtium.c as C, never with a C++ compiler (the extern symbols would be mangled).
- -lservirtium_vcr needs the plain file name libservirtium_vcr.so/.dylib; the versioned release asset name does not satisfy it.
- The library must be at least as new as servirtium.c: use the same tag for both, or the link fails with undefined reference to aether_vcr_embed_...
- A recording closed with neither sv_close_and_flush nor sv_discard writes nothing and sv_close returns an error.
- macOS: the release dylib's install name is a Linux CI path; run install_name_tool -id @rpath/... (and codesign -f -s -) on your copy before linking.
- Windows: take the .dll asset with its .dll.lib import library by hand.

References:
- This language's page: https://servirtium.dev/languages/c/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/c
- 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

macOS arm64: downloaded libservirtium_vcr-v2.0.0-alpha.20-macos-arm64.dylib from the release, sha256 OK; vendored servirtium.h/.c from the repo at v2.0.0-alpha.20 and compiled a record-then-replay program (playback tapes/single_get.md as the stand-in upstream, sv_record + sv_redact_credentials + sv_note + sv_close_and_flush, then sv_playback of the new tape: live ok-body, replay ok-body, last_kind SV_OK); sv_close on an unflushed recorder returned the 'without commit or discard' error and wrote nothing; sv_discard wrote nothing; the extras (redact_header, redact_credential_header, volatile_rule, rewrite_upstream_base, discard) ran. The release dylib failed to load until its install name was rewritten (see doc problems).

They mirror these files in the repository:

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