Scalar Mock Server Skill
Reference for implementing and debugging mock APIs with @scalar/mock-server.
Use this when you need realistic API responses from an OpenAPI description document, custom request behavior, seeded data, or Docker-based mock environments.
Overview
- Package:
@scalar/mock-server
- Runtime: Node.js (package engine:
>=22)
- Main API:
createMockServer(options)
- Docs:
Quick Start
Fastest way to run a mock server from a local OpenAPI description:
npx @scalar/cli document mock openapi.json --watch
Programmatic setup:
import { serve } from '@hono/node-server'
import { createMockServer } from '@scalar/mock-server'
const app = await createMockServer({
document: './openapi.yaml',
onRequest({ context, operation }) {
console.log(context.req.method, context.req.path, operation.operationId)
},
})
serve({ fetch: app.fetch, port: 3000 })
createMockServer() Options
At least one of the following is required:
document: OpenAPI description document as URL, file path, or object
specification: deprecated alias for document
Optional:
onRequest({ context, operation }): callback before each request is processed
validateRequest: set to false to always return a mock response instead of rejecting contract violations with 422 (default: true)
logger: pass false to silence the authentication instructions printed on startup, or a (line) => void sink to redirect them (default: logs to the console)
Built-in Behavior
When the server starts, it:
- Processes and loads the OpenAPI description document.
- Seeds schema data from
x-seed extensions (idempotent: only when collection is empty).
- Registers authentication routes for declared security schemes.
- Registers operation routes for each path + method.
- Exposes the source document at:
/openapi.json
/openapi.yaml
Custom Request Logic with x-handler
Use x-handler in an operation for dynamic behavior instead of static examples.
Helpers available in x-handler:
store for in-memory persistence (list, get, create, update, delete, clear)
faker for generated test data
req for request data (body, params, query, headers)
res for response examples by status code (res['200'], res['404'], ...)
Status behavior:
store.get() / store.update() => 200 when found, 404 when not found
store.create() => 201
store.delete() => 204 when deleted, 404 when not found
store.list() => 200
- Returning
null/undefined triggers 404 (uses responses.404 example/schema when provided)
Seed Data with x-seed
Use x-seed on components.schemas.<SchemaName> to seed initial data at startup.
Helpers available in x-seed:
seed.count(n, factory)
seed(array)
seed(factory) (single item shortcut)
faker, store, and schema
Key rule: the schema key name is used as the collection name.
Docker Usage
Run the Docker image:
docker run -p 3000:3000 scalarapi/mock-server --url https://api.example.com/openapi.yaml
Document source priority (high to low):
--url <URL>
OPENAPI_DOCUMENT
OPENAPI_DOCUMENT_URL
/docs volume-mounted files
Useful routes:
- Mock endpoints: from your OpenAPI paths
- API reference UI:
/scalar
- Description document:
/openapi.json, /openapi.yaml
Troubleshooting Checklist
- Confirm the OpenAPI description document is valid and reachable.
- Confirm at least one document source is configured (
document, --url, env var, or mounted file).
- If seeded data is missing, check
x-seed exists on schema keys and the collection was empty on startup.
- If auth-protected routes return unauthorized responses, verify matching
securitySchemes and request credentials.
- If custom logic fails, inspect
x-handler runtime errors (mock server returns 500 with handler error details).
- For an unhandled
500 (one whose error is Internal Server Error), read its operation object — it names the method and OpenAPI path that failed, plus the operationId when the document declares one.
1---2name: mock-server3description: Build, customize, and troubleshoot OpenAPI mock servers with @scalar/mock-server, including x-handler, x-seed, authentication, and Docker.4---56# Scalar Mock Server Skill78Reference for implementing and debugging mock APIs with `@scalar/mock-server`.9Use this when you need realistic API responses from an OpenAPI description document, custom request behavior, seeded data, or Docker-based mock environments.1011## Overview1213- Package: `@scalar/mock-server`14- Runtime: Node.js (package engine: `>=22`)15- Main API: `createMockServer(options)`16- Docs:17 - Getting started: <https://scalar.com/tools/mock-server/getting-started>18 - Custom request handlers (`x-handler`): <https://scalar.com/tools/mock-server/custom-request-handler>19 - Data seeding (`x-seed`): <https://scalar.com/tools/mock-server/data-seeding>20 - Docker: <https://scalar.com/tools/mock-server/docker>2122## Quick Start2324Fastest way to run a mock server from a local OpenAPI description:2526```bash27npx @scalar/cli document mock openapi.json --watch28```2930Programmatic setup:3132```ts33import { serve } from '@hono/node-server'34import { createMockServer } from '@scalar/mock-server'3536const app = await createMockServer({37 document: './openapi.yaml',38 onRequest({ context, operation }) {39 console.log(context.req.method, context.req.path, operation.operationId)40 },41})4243serve({ fetch: app.fetch, port: 3000 })44```4546## `createMockServer()` Options4748At least one of the following is required:4950- `document`: OpenAPI description document as URL, file path, or object51- `specification`: deprecated alias for `document`5253Optional:5455- `onRequest({ context, operation })`: callback before each request is processed56- `validateRequest`: set to `false` to always return a mock response instead of rejecting contract violations with `422` (default: `true`)57- `logger`: pass `false` to silence the authentication instructions printed on startup, or a `(line) => void` sink to redirect them (default: logs to the console)5859## Built-in Behavior6061When the server starts, it:62631. Processes and loads the OpenAPI description document.642. Seeds schema data from `x-seed` extensions (idempotent: only when collection is empty).653. Registers authentication routes for declared security schemes.664. Registers operation routes for each path + method.675. Exposes the source document at:68 - `/openapi.json`69 - `/openapi.yaml`7071## Custom Request Logic with `x-handler`7273Use `x-handler` in an operation for dynamic behavior instead of static examples.7475Helpers available in `x-handler`:7677- `store` for in-memory persistence (`list`, `get`, `create`, `update`, `delete`, `clear`)78- `faker` for generated test data79- `req` for request data (`body`, `params`, `query`, `headers`)80- `res` for response examples by status code (`res['200']`, `res['404']`, ...)8182Status behavior:8384- `store.get()` / `store.update()` => `200` when found, `404` when not found85- `store.create()` => `201`86- `store.delete()` => `204` when deleted, `404` when not found87- `store.list()` => `200`88- Returning `null`/`undefined` triggers `404` (uses `responses.404` example/schema when provided)8990## Seed Data with `x-seed`9192Use `x-seed` on `components.schemas.<SchemaName>` to seed initial data at startup.9394Helpers available in `x-seed`:9596- `seed.count(n, factory)`97- `seed(array)`98- `seed(factory)` (single item shortcut)99- `faker`, `store`, and `schema`100101Key rule: the schema key name is used as the collection name.102103## Docker Usage104105Run the Docker image:106107```bash108docker run -p 3000:3000 scalarapi/mock-server --url https://api.example.com/openapi.yaml109```110111Document source priority (high to low):1121131. `--url <URL>`1142. `OPENAPI_DOCUMENT`1153. `OPENAPI_DOCUMENT_URL`1164. `/docs` volume-mounted files117118Useful routes:119120- Mock endpoints: from your OpenAPI paths121- API reference UI: `/scalar`122- Description document: `/openapi.json`, `/openapi.yaml`123124## Troubleshooting Checklist125126- Confirm the OpenAPI description document is valid and reachable.127- Confirm at least one document source is configured (`document`, `--url`, env var, or mounted file).128- If seeded data is missing, check `x-seed` exists on schema keys and the collection was empty on startup.129- If auth-protected routes return unauthorized responses, verify matching `securitySchemes` and request credentials.130- If custom logic fails, inspect `x-handler` runtime errors (mock server returns `500` with handler error details).131- For an unhandled `500` (one whose `error` is `Internal Server Error`), read its `operation` object — it names the method and OpenAPI path that failed, plus the `operationId` when the document declares one.