Swagger/OpenAPI mock server

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.


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.


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.


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

  1. Have a swagger configuration ready in a file in the folder config/petstore.yaml

     swagger: "2.0"
     info:
     version: 1.0.0
     title: Swagger Petstore
         .
         .
         .
    

You can copy the example swagger specification from here.

  1. 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.

  2. Run Imposter with Docker

    Ensure that your shell is placed in the parent directory of config and run

     docker run -ti -p 8443:8443 \
     -v $(pwd)/config:/opt/imposter/config \
     outofcoffee/imposter-openapi
    
  3. Open http://localhost:8443/_spec/ to play with your Mock Server and Swagger UI

    Imposter Mock API