OpenAPI mock servers: start client development from a specification
An API mock server is a test HTTP server that stands in for a real business service. It uses an OpenAPI description and examples to serve paths and responses, so client work can begin before the backend is ready and gaps in the contract can be found early. Mock responses do not prove persistence, authorization, transactions, or real service behavior.
An API mock server is a test HTTP server that stands in for a real business service. It uses an OpenAPI description and examples to serve paths and responses, so client work can begin before the backend is ready and gaps in the contract can be found early. Mock responses do not prove persistence, authorization, transactions, or real service behavior.
What can it help you check?
A specification-driven mock gives client developers request shapes and response models before the API is implemented. Stoplight Prism uses OpenAPI operations, request validation rules, and response examples to provide an HTTP mock. Its validation proxy is a separate feature for checking a running implementation.
| Question | What a mock can do | What still needs checking |
|---|---|---|
| Is there a route the client can call? | Respond for paths and methods in the description | Real server routing and deployment |
| Does a request fit the contract? | Validate requests against description rules | Validation and authorization in production code |
| Can the client process the response model? | Return example- or schema-based fake data | Whether the implementation conforms |
| Can the client build error paths? | Select documented error statuses/examples | Whether the real server uses the same conditions and bodies |
Distinguish a mock from a sandbox. A mock usually synthesizes responses from a description and does not keep state. A sandbox may expose some real implementation, test data, or accounts, so capabilities and side effects vary. To check request/response mismatches against a real implementation, use a validation proxy such as Prism’s to compare traffic with the description.
Better examples produce better mocks
Mock results depend on the description. In Prism’s default static mode, an explicit response example is preferred. Without one, values are generated from schema defaults, examples, nullability, formats, and other properties. A string without an example may be returned as a placeholder such as "string"; that is a type-shaped value, not realistic business data. Add representative success, empty-result, and error examples when the client needs meaningful screens.
Static responses make repeated checks predictable. Dynamic responses vary values according to the schema and can exercise client handling of different sizes or values, but may be unsuitable when a test expects the same output each time. Choose the mode for the test purpose; when checking a particular example, select its response status and example together.
Run a local mock
The commands below follow Prism OSS CLI 3.x documentation. The repository README states a Node.js requirement of at least 18.20.1, but check the installation page for the exact Prism release before installing. These commands are documented examples; they were not run for this article.
Prepare the project’s OpenAPI file and review client paths/methods, request schemas, response codes, and examples.
Install @stoplight/prism-cli using its official installation guide, then start the mock from the project directory:
prism mock openapi.yamlBy default Prism listens at http://127.0.0.1:4010 and lists available operations.
Point curl or the client’s base URL at the local server. Inspect status, content type, and body. The response is synthesized from the local description and examples.
Add error examples to the description and use the documented selection options to check client error handling. Prism documents Prefer header options for selecting a status code and example.
In Docker, the default localhost binding may prevent access from outside the container. Only if container-to-container access is needed, check the documented host option and expose the server on the required interface.
Expected result: the client sends a request declared in the description and processes the configured example or schema-based response. If you get a 404, compare Prism’s printed routes with OpenAPI paths, servers, and base path. If varying responses make checks flaky, use static mode and explicit examples.
When the real API is ready
A correct mock does not show that the provider follows the contract. Once the API is implemented, change the base URL to the real development server and, where useful, compare requests and responses with the description through a validation proxy. If they differ, first decide whether the description is the intended contract; then update the description and provider as needed and rerun client checks.
Before you start
Does the mock tool support your OpenAPI version and required features?
Does the description include representative success, error, and empty-result examples?
Does the client team understand fake values and lack of state?
Have you checked local host, port, and Docker port exposure?
Is there a later contract check against the real API?
Sources
[Prism: HTTP mocking and static/dynamic response generation](https://github.com/stoplightio/prism/blob/main/docs/guides/01-mocking.md)
[Prism: CLI, response examples, and error selection](https://github.com/stoplightio/prism/blob/main/docs/getting-started/03-cli.md)
[Prism: validation proxy for checking a real API against OpenAPI](https://github.com/stoplightio/prism/blob/main/docs/guides/03-validation-proxy.md)
[Prism: supported specifications and installation/local usage](https://github.com/stoplightio/prism)