Mock server
npx doctreen mock — spec-driven fake API with CRUD short-circuits.
Spin up an Express-backed fake of your API in seconds — no spec stitching, no
manual handler stubs. doctreen mock reads any OpenAPI 3.x document (from a
live /docs URL or a local JSON file) and synthesises responses from each
operation's schema.
Languages: ships in the npm package and the PHP package
(vendor/bin/doctreen mock — CRUD short-circuits, latency/error injection,
optional Faker). Because it feeds on any OpenAPI 3.x document, either CLI can
mock a service written in any language. A native Python mock server is
planned.
# From a running doctreen-enabled server
npx doctreen mock --from http://localhost:3000/docs --port 4000
# From an OpenAPI file on disk
npx doctreen mock --from ./openapi.json --port 4000
# With latency + random error injection (shake out frontend error paths)
npx doctreen mock --from ./openapi.json --latency 100-500 --error-rate 0.1PHP projects get the same command from Composer:
vendor/bin/doctreen mock --from https://api.example.com/docs --port 4000What you get
- Schema-faithful responses generated from each operation's success response.
$ref,oneOf/anyOf/allOf,enum,const,default, and per-operationexamplesare all respected. - CRUD short-circuits. When the spec exposes
/resource+/resource/:id,POSTactually creates a row in an in-memory store,GETreads it back,PATCH/PUTmutate it,DELETEremoves it. Envelope responses ({ products: [...], total, filters }) are detected and the array is swapped in. - Realistic values via Faker (optional). Install
@faker-js/fakerand fields namedemail,name,uuid,createdAt, … plus OpenAPIformats (uuid,email,date-time,uri,ipv4, …) get realistic values. Without it, output falls back to deterministic placeholders. - Latency + error injection.
--latency 200for a fixed delay,--latency 100-500for a random range.--error-rate 0.1returns a randomly-picked declared 4xx/5xx response 10 % of the time. - Optional persistence.
--persist ./fixtures.jsonwrites the in-memory store to disk on every mutation and loads it on restart.
Flags
| Flag | Purpose |
|---|---|
--from <src> | URL (auto-appends /openapi.json) or local JSON file. Required. |
--port <n> | Listen port. Default 4000. |
--host <addr> | Bind address. Default 0.0.0.0. |
--latency <ms|a-b> | Fixed ms (200) or random range (100-500). |
--error-rate <p> | 0..1 probability of returning a declared error response. |
--seed <n> | Faker seed for deterministic output. |
--persist <file> | JSON file to persist CRUD state across restarts. |
--no-crud | Disable in-memory CRUD; always return synthesised examples. |
--no-faker | Disable Faker even when installed. |
--quiet | Suppress per-request log lines. |
Programmatic API
Node-only — on PHP use the vendor/bin/doctreen mock CLI, which exposes the
same flags.
const { startMockFromOpenApi } = require('doctreen/mock');
const { app, server, routeCount } = await startMockFromOpenApi({
from: './openapi.json',
port: 4000,
latency: [100, 500],
errorRate: 0.05,
});
// later
server.close();Schema → example helper
The generator powering the mock server (and the docs UI's Copy-as-cURL / Postman buttons) is exported on its own:
const { generateExample } = require('doctreen/example');
generateExample(
{ type: 'object', properties: { email: { type: 'string', format: 'email' } } },
{ faker: true }
);
// → { email: 'Ada.Lovelace@example.com' }Accepts both doctreen SchemaNode and OpenAPI Schema Objects. Pass
components to resolve $refs.