lazyapi mock
Serves a spec as a fake API, so tests, examples, CI and agents have a server to talk to before (or without) the real one.
lazyapi mock <spec.yml> [--port 4010] [--host 127.0.0.1] [--no-validate] [--quiet]Responses
For each request, the mock finds the operation (literal paths win over templates: /users/me before /users/{id}). Path prefixes from the spec's servers (e.g. /v1) are accepted too. The mock then answers with:
- Status: the code asked for with
Prefer: code=404, otherwise the lowest declared 2xx, otherwise the lowest declared code. - Media type: one that matches
Accept, preferring JSON. - Body: the named example asked for with
Prefer: example=<name>. Otherwise, in order: the media typeexample, the first named example, a sample generated from the schema (honouringexample,default,enum,formatand minimums). - Headers: declared response headers that have an example or schema.
HEAD gets headers only. OPTIONS on a path without an OPTIONS operation answers a CORS preflight.
Validation
Requests are validated against the operation (parameters and body). A request that does not match gets 422:
{"error": "request does not match the OpenAPI spec", "details": "body: name: property \"name\" is missing", "mock": "lazyapi"}Pass --no-validate to answer anyway. Unknown paths get 404, undeclared methods 405 (with Allow). Each request is logged to stderr: GET /users/1 → 200 getUser (41µs).
Testing against the mock
lazyapi mock api.yml &
lazyapi run api.yml --server http://127.0.0.1:4010 --methods allInside test scripts, Prefer lets you exercise error paths the real server is hard to trigger:
const missing = lazyapi.send("getUser", { params: { id: "999" }, headers: { Prefer: "code=404" } });
expect(missing).toHaveStatus(404);
expect(missing).toMatchSchema();examples/openapi.yml and its tests/ run this way in CI.