# Mock server

> Serve an API doc's operations on your computer, answered from saved examples, the spec's examples or samples, with each request checked against the spec.

A mock server answers calls to your API before the API exists, so a frontend can be built against it. It runs on your computer, from the Dockito desktop app or the CLI. Requests to it never reach Dockito's servers.

## In the desktop app

Open an API doc and pick **Mock server** in the doc's ⋯ menu, then **Start**. While it runs, **Mock** in the doc bar opens it again. It listens on `http://127.0.0.1:4010`, or any free port when 4010 is taken; type a port to pick your own. **Copy** takes the URL.

**Use as baseUrl** puts the mock's URL into an environment named Mock, as `baseUrl`, so the doc's requests can send to it. Pick Mock in the environment picker. Open **Requests** first; the button needs it.

The mock keeps running when you go to another doc, and stops when you press **Stop** or quit the app.

## From the CLI

```sh
dockito mock bookings-api            # an API doc in Dockito, by its link or code
dockito mock openapi.yaml --port 0    # a spec on disk, on any free port
```

`--port` picks the port (4010 by default), `--delay` waits before every answer, in ms, and `--json` prints each request as one JSON line, for scripts and AI agents. Stop it with Ctrl+C.

## What it answers

For each request, the first that fits:

1. **A saved example** from the doc's requests, matched the way Postman matches: the method, then the path, then the query and body.
2. **The spec's example** for the operation's first success response.
3. **A sample** made from the response's schema.

Every answer says which in its `X-Dockito-Mock` header: `saved`, `example`, `sample` or `empty`.

## Requests checked against the spec

Each request is checked as it comes: required parameters, their types, the credentials the operation needs and the body against its schema. A request that doesn't fit gets a **400** with the problems in words, and `X-Dockito-Mock: invalid`:

```json
{
  "error": "The request doesn't fit the spec",
  "problems": ["the request has no credentials the spec accepts; it needs `bearerAuth` (an `Authorization: Bearer …` header)"],
  "more": 0
}
```

A path the spec doesn't have gets a 404, and a method it doesn't take gets a 405, each saying why.

## Headers you can send

| Header | What it does |
|---|---|
| `x-mock-response-code: 404` | Answers with that status's example or sample |
| `x-mock-response-name: Sold out` | Answers with the saved example of that name |
| `x-mock-delay: 1500` | Waits that many ms before this answer (up to 60 s) |
| `x-mock-validate: false` | Skips the check for this request |

The `x-mock-response-*` headers are Postman's, so a frontend written against a Postman mock works here too.

## The log

The desktop app lists each request with a pass or a fail in words: `201 from the spec's example`, or a 400 with the first problem, with the time it took. The CLI prints the same lines.

## Changes while it runs

Publish a new version of the spec, or save a new example, and the next request answers from it. There's no restart. The CLI does the same, and with a spec on disk it follows the file.

## Calls from a browser

The mock answers any page's cross-origin calls, so a frontend on `localhost:5173` can call it straight away.
