prism mock: Mock an API from OpenAPI
prism mock starts a local HTTP server that responds to requests based on an OpenAPI description, using examples or generated data.
Stoplight Prism turns a spec into a live mock so front-end and integration tests can run against a contract before the real backend exists. It also validates requests against the spec.
What it does
prism mock loads an OpenAPI/Postman document and serves each operation, returning the response example when present or dynamically generated data with --dynamic. It validates incoming requests and returns violations as errors.
Common usage
prism mock openapi.yaml
# bind to all interfaces on a fixed port for CI containers
prism mock -h 0.0.0.0 -p 4010 openapi.yaml
# generate data from schemas instead of static examples
prism mock --dynamic openapi.yamlOptions
| Flag | What it does |
|---|---|
| -p, --port <n> | Port to listen on (default 4010) |
| -h, --host <addr> | Host/interface to bind (use 0.0.0.0 in containers) |
| -d, --dynamic | Generate response data from schemas |
| --cors | Enable CORS headers |
| --errors | Return validation errors for invalid requests |
| -m, --multiprocess | Run the validator in a separate process |
In CI
Start prism mock -h 0.0.0.0 -p 4010 openapi.yaml & as a background service, wait for the port, then run your client/contract tests against it and kill it after. Binding to 0.0.0.0 is essential in Docker-based runners or the mock is unreachable from the test container.
Common errors in CI
"No path matched" or NO_PATH_MATCHED_ERROR means the request URL is not in the spec. "Invalid request" 422 with a UNPROCESSABLE_ENTITY/violations body means the request body or params fail schema validation. "Address already in use" means the port is taken; change -p. On start, The provided document ... is invalid means the OpenAPI file itself does not validate; lint it first.