Mock an API from its OpenAPI spec

At a glance
Goal: Mock an API based on its OpenAPI spec without writing mocks
Time: 5 minutes
Plugins: OpenApiMockResponsePlugin
Prerequisites: Set up Dev Proxy, an OpenAPI spec for the API

When you build an app against an API that isn't ready yet, or that you can't call during development, you often have its OpenAPI spec. Instead of writing mock responses by hand, you can use Dev Proxy to mock the API based on its spec. Dev Proxy uses examples defined in the spec, or generates responses from the response schemas defined for each operation.

Create an OpenAPI spec

Start with the OpenAPI spec of the API that you want to mock. Make sure that the spec defines the servers where the API is hosted, because Dev Proxy uses them to match intercepted requests to the operations in the spec.

File: openapi.yaml

openapi: 3.0.1
info:
  title: Contoso Customers API
  version: 1.0.0
servers:
  - url: https://api.contoso.com/v1
paths:
  /customers:
    get:
      description: Get all customers
      responses:
        '200':
          description: List of customers
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Customer'
  /customers/{id}:
    get:
      description: Get a customer by ID
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        '200':
          description: Customer
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Customer'
              example:
                id: 1
                name: Contoso
                email: info@contoso.com
components:
  schemas:
    Customer:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        email:
          type: string
          format: email

If you don't have an OpenAPI spec for the API yet, you can generate one from your app's traffic.

Configure Dev Proxy

In your Dev Proxy configuration file, enable the OpenApiMockResponsePlugin, point it to your spec, and add the API's URL to urlsToWatch.

File: devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/rc.schema.json",
  "plugins": [
    {
      "name": "OpenApiMockResponsePlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "openApiMock"
    }
  ],
  "urlsToWatch": [
    "https://api.contoso.com/*"
  ],
  "openApiMock": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/openapimockresponseplugin.schema.json",
    "specFile": "openapi.yaml"
  }
}

The specFile property accepts a path relative to the configuration file or an http or https URL. When you use a local file, Dev Proxy reloads the spec when you change it.

Call the mocked API

Start Dev Proxy and call the API. For example, use curl:

devproxy
curl -ikx http://127.0.0.1:8000 https://api.contoso.com/v1/customers/1

Dev Proxy responds with the example defined in the spec for the GET /customers/{id} operation. When you call https://api.contoso.com/v1/customers, which doesn't have an example, Dev Proxy generates the response from the schema. Requests to URLs that you watch but that aren't defined in the spec pass through to the real API.

Generate realistic responses using a language model

Responses that Dev Proxy generates from schemas contain placeholder values. To get responses with realistic data, configure Dev Proxy to use a local language model.

File: devproxyrc.json

{
  "languageModel": {
    "enabled": true
  }
}

When the language model is enabled, Dev Proxy uses it to generate JSON responses for operations that don't define examples in the spec. It prompts the language model with the request, the operation's description, and the response schema, so that the values match the meaning of each property, for example, company names in name and email addresses in email.

Dev Proxy caches each generated response in memory, so subsequent requests to the same operation return the same response without calling the language model again. Dev Proxy clears the cache when you change the spec or restart Dev Proxy.

Next step

Learn more about the OpenApiMockResponsePlugin.

See also