OpenAPI Mocker is a robust mock server designed to generate API responses based on an OpenAPI 3.x specification (both YAML and JSON formats). It provides a quick way for developers to establish a local mock API, primarily through a command-line interface (CLI) or Docker container, but also offers a programmatic API. The current stable version, 2.0.0, represents a significant rewrite in TypeScript, enhancing maintainability and type safety. Key capabilities include comprehensive request parameter and body validation, dynamic response generation derived from schema examples, flexible response selection via `Prefer` HTTP headers (e.g., `statusCode=XXX`, `example=name`), and advanced data customization through `x-faker` and `x-count` extensions for generating realistic, randomized data. Its primary differentiator is the rich support for OpenAPI 3.0 features and custom extensions, making it ideal for rapid API prototyping, frontend development, and testing in environments where a backend is not yet available or stable.
npm install open-api-mockerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically initialize and start the OpenAPI Mocker server using an in-memory YAML schema, leveraging `x-faker` to generate dynamic response data, and then gracefully stopping it. It requires creating a temporary schema file.
Refer to the `faker` library documentation for correct method signatures and ensure `x-faker` values match these. Example: `x-faker: name.firstName` or `x-faker: 'random.number({ "min": 1, "max": 20 })'`.Review your programmatic usage and ensure it aligns with the officially documented `OpenApiMocker` class and its constructor options. Re-verify type definitions if using TypeScript.
Validate your OpenAPI schema using tools like `spectral` or online validators before passing it to the mocker. Simplify complex parts of the schema if response generation becomes problematic.
Use `-v "$PWD/my-schema.yaml:/app/schema.json"` for mounting from the host, and then `docker run ... jormaechea/open-api-mocker -s /app/schema.json` to tell the mocker where to find it inside the container.
Run `npm install open-api-mocker` or `yarn add open-api-mocker`. If using TypeScript, ensure `esModuleInterop` is true in `tsconfig.json` for CommonJS imports, though named imports are preferred for this ESM-first library.
Carefully review your `schema.yaml` or `schema.json` file for syntax errors and validate it against the OpenAPI 3.x specification using an external validator (e.g., `spectral lint your-schema.yaml` or online OpenAPI validators).
Check your client's request URL and method against your OpenAPI schema's `paths` section. Ensure paths match exactly, including any base paths defined in the `servers` object of your schema.
Choose a different port for the mocker using the `-p <port>` CLI option or the `port` option in the programmatic `OpenApiMocker` constructor (e.g., `open-api-mocker -p 8081` or `new OpenApiMocker({ port: 8081 })`).No dependency data recorded yet.