I have an API reference in a Swagger file. I want to create a very simple mock server, so that when I call e.g.:
mymockurl.com/users it will return a predefined JSON (no need to connect to a database).
What's the easiest way to do this? I'm not a backend guy.
13 Answers 13
An easy way to create simple mock from an OpenAPI (fka Swagger) spec without code is to use a tool call prism available at http://github.com/stoplightio/prism written in Typescript.
This command line is all you need:
./prism run --mock --list --spec <your swagger spec file>
The mock server will return a dynamic response based on the OpenAPI spec. If examples are provided in the spec, prism will return them, if not it will generate dummy data based on the spec.
Edit (Aug 2020):
The command has changed in the latest version. The following will do:
prism mock <your spec file>
It accepts swagger and postman doc as well.
8 Comments
SwaggerHub provides a mock server for OpenAPI 2.0 and 3.0 specs. Mocking is supported on both free and paid plans.
To use the mock server, import your spec into SwaggerHub and enable "API Auto Mocking". Mock responses can be JSON, YAML and XML, and are generated based on your response schemas and the example, default and enum values defined in the schemas.
Disclosure: I work for the company that makes SwaggerHub.
7 Comments
examples contain multiple examples, the first example will be used". Please feel free to submit a feature request. (However, randomized responses are supported in our ReadyAPI Virtualization product if that's an option for you.)Imposter is a a scriptable, multipurpose mock server written in Java.
Very easy to setup in a Docker environment and provides a Swagger UI to play with your mock api.
Let's see an example setup
Have a swagger configuration ready in a file in the folder
config/petstore.yamlswagger: "2.0" info: version: 1.0.0 title: Swagger Petstore . . .
You can copy the example swagger specification from here.
Create a configuration file for Imposter in
config/openapi-plugin-petstore-config.json{ "plugin": "com.gatehill.imposter.plugin.openapi.OpenApiPluginImpl", "specFile": "petstore.yaml" }Name of the configuration file has to end with
-config.json.Run Imposter with Docker
Ensure that your shell is placed in the parent directory of
configand rundocker run -ti -p 8443:8443 \ -v $(pwd)/config:/opt/imposter/config \ outofcoffee/imposter-openapiOpen http://localhost:8443/_spec/ to play with your Mock Server and Swagger UI
Imposter Mock API
7 Comments
Given the OpenAPI/Swagger spec, you can use Swagger Codegen to generate server stub in different server frameworks (e.g. Java Spring. PHP Slim, Ruby on Rails5, etc).
Here is the related documentation:
https://github.com/swagger-api/swagger-codegen/wiki/Server-stub-generator-HOWTO
UPDATE: In May 2018, about 50 top contributors of Swagger Codegen decided to fork the project to create a community-driven version called OpenAPI Generator. Please refer to the Q&A for the reasons behind the fork.
3 Comments
The Mock-Server project supports creating stubs based on Swagger/OpenAPI Specs.
Comments
Here is the docker container for mock api server from swagger yaml.
docker run -i \
-p 8000:8000 \
-v /path/to/file.yaml:/data/swagger.yaml \
-t palo/swagger-api-mock:latest
This is internally using swagger-mock-api
Comments
I docker composed Swagger Editor, Swagger UI and Swagger mock api server to handle them more easily. Check it out. There is a sample swagger spec in this so the Editor, UI and the mock API server will run without any configuration from the start. All you need to do is edit the swagger spec, save swagger.json and swagger.yaml, and restart docker.
1 Comment
I created mock api server myself that can server swagger.json file. It is very easy to setup locally if you have python installed.
Have a look at this: https://github.com/bikcrum/Mock-API-server
Just use this command to serve your swagger.json file which will create API endpoints based on specification on the swagger file.
Quick and straightforward
python app.py -s /path/to/swagger.json
Extended options
usage: app.py [-h] -s SOURCE [-p PORT] [-t {swagger}] [-sc STATUS_CODE] [-r RANDOM_SEED] [-d DEFAULT_VALUE [DEFAULT_VALUE ...]]
[-l LIST_SIZE [LIST_SIZE ...]]
Options for mock responses
Required and optional arguments:
-h, --help show this help message and exit
-s SOURCE, --source SOURCE
(Required) API reference source file path.
-p PORT, --port PORT (Optional,default=5000) Port number the app runs on.
-t {swagger}, --type {swagger}
(Optional,default='swagger') Type of API reference. Currently only supports Swagger.
-sc STATUS_CODE, --status_code STATUS_CODE
(Optional,default=200) Generates responses with status code provided.
-r RANDOM_SEED, --random_seed RANDOM_SEED
(Optional) Generates random responses based on seed value.
-d DEFAULT_VALUE [DEFAULT_VALUE ...], --default_value DEFAULT_VALUE [DEFAULT_VALUE ...]
(Optional) Sets default values in response body. Format key=value.
-l LIST_SIZE [LIST_SIZE ...], --list_size LIST_SIZE [LIST_SIZE ...]
(Optional,default=[2]) Sets default size of list in response body.
Comments
Recently I have come across Microcks.io and it has helped me solve number of problems. Also found Apicur.io for editing/creating swagger files. A standard worth embrasing.
Comments
Use Mockoon, "Mockoon is the easiest and quickest way to design and run mock REST APIs. No remote deployment, no account required, free and open-source." It has the best user-friendly GUI in my opinion. It also has a CLI and Serverless function option. You can generate mock endpoints from your Swagger/Open API files and export them after you've made changes.
There are many other features: Multiple responses per route, File serving, etc. See https://mockoon.com/features/ for details.
You can see Mockoon's comparison to other products here: https://mockoon.com/compare/
Comments
MockLab now supports auto-generation of mock APIs from an imported Swagger definition. And you can also set it up as a webhook received in Swaggerhub so it gets updated on every save/publish:
https://www.mocklab.io/blog/mocklab-now-supports-swagger-and-swaggerhub/
Comments
openapi-mock is a CLI wrapper for swagger-node-runner and sway specifically to start a mock server from an openapi/swagger spec file.
1 Comment
Beeceptor has an OpenAPI mock server. This is powered by AI/LLM for test data geneations. This marries the mock APIs along with test data generation in a unified approach where you upload the spec and get hosted/live APIs that respond like real APIs.
- Mock API server from spec's schema components
- Real life test data generation on every API invocation.
- [Hosted] Server can be created either by uploading the spec file, or from a public URL (e.g. docs)
Comments
Explore related questions
See similar questions with these tags.