What is Prism (Stoplight)?
Prism is an open-source (Apache 2.0) HTTP mock server and validation proxy from Stoplight, which is now part of SmartBear. Point prism mock at an OpenAPI 2.0, 3.0 or 3.1 document (or a Postman collection) and it serves every operation in it, validating each incoming request against the description and generating responses from its examples or schemas. Point prism proxy at the same document and a real server, and it forwards traffic while reporting any request or response that does not match the contract. It is a Node.js CLI, also published as a Docker image.
Why use it in the API Lifecycle?
- Mock from the description, with no code: Any valid OpenAPI document becomes a running server, so client teams can build against an API before it exists.
- Request validation: Invalid requests get a contract-correct error instead of a happy response: a security failure maps to a documented
401 Unauthorized, a body or parameter error to a documented422 Unprocessable Contentor400 Bad Request, and an unknown route to404 Not Found, with Problem Details bodies. - Content negotiation: Honors
AcceptandContent-Typelike a real server, and fails when a client asks for a media type the operation does not define. - Static or dynamic responses: Static mode returns examples (or values built from defaults and formats);
-dgenerates random data from the schema instead. - Response selection with
Prefer: Clients can choosePrefer: code=404,Prefer: example=dogorPrefer: dynamic=trueper request, or the__code-style query parameters when they cannot set headers. - Validation proxy:
prism proxyreports contract violations in ansl-violationsresponse header, or with--errorsturns them into Problem Details responses.
Best Practices
- Run
prism proxy openapi.yaml http://localhost:3000 --errorsin front of the real service during integration tests. Every response the implementation sends that does not match the description then fails the test, which is the cheapest way to keep the document and the code from drifting. - Without
--errors, the proxy only addssl-violationsand passes traffic through unchanged. Use that mode to observe a staging environment without breaking it, and log the header. - Name your examples and select them with
Prefer: example=<name>in frontend tests, and always pair it withcode=as well: if Prism switches status because the request failed validation, an example for the wrong status is ignored. - Use
Prefer: code=to exercise error handling:429 Too Many Requests,503 Service Unavailableand every documented 4xx the client must handle. If Prism cannot return a status, the description does not document it, which is itself a finding. - Prefer static mode for automated tests, because random data makes assertions flaky; use
-dfor exploratory work and to surface schemas that are too loose (a free-formstringwith noformatormaxLengthproduces visibly useless data). - Set
NODE_ENV=productionor pass-mwhen running Prism in a container, so log processing runs in a separate process and does not slow request handling. - Remember that a mock only proves the description is internally consistent. Pair it with contract or property-based tests against the real implementation, as covered in Testing APIs.