CLI Mock Server

April 4, 2026 · View on GitHub

Start a mock API server from the command line. Point it at an OpenAPI spec and get a working server.

bun install -g @schmock/cli

Usage

schmock petstore.yaml
[@schmock/cli] Loaded spec: petstore.yaml
[@schmock/cli] Detected 3 CRUD resources
[@schmock/cli] Server running at http://127.0.0.1:3000

Options

schmock <spec> [options]
FlagDescriptionDefault
--port <number>Port to listen on3000
--hostname <host>Hostname to bind to127.0.0.1
--seed <path>JSON file with seed data
--corsEnable CORS headersfalse
--debugEnable debug loggingfalse
--seed-random <number>Deterministic data generation
--errorsEnable request validationfalse
--watchWatch spec file for changesfalse
--adminEnable admin API endpointsfalse
-h, --helpShow help

Examples

With seed data

Create a seed.json:

{
  "users": [
    { "userId": 1, "name": "Alice", "email": "alice@example.com" },
    { "userId": 2, "name": "Bob", "email": "bob@example.com" }
  ],
  "posts": { "count": 20 }
}
schmock api.yaml --seed seed.json --port 8080

CORS for frontend development

schmock api.yaml --cors --port 4000

Deterministic data

schmock api.yaml --seed-random 42
# Same data every time with the same seed

Watch mode

schmock api.yaml --watch
# Server reloads when the spec file changes

Admin API

When started with --admin, additional endpoints are available:

EndpointDescription
GET /schmock-admin/routesList all registered routes
GET /schmock-admin/stateGet current shared state
GET /schmock-admin/historyGet request history
POST /schmock-admin/resetReset state and history

Programmatic Usage

import { createCliServer } from '@schmock/cli'

const server = await createCliServer({
  spec: './petstore.yaml',
  port: 8080,
  cors: true,
  seed: './seed.json',
})

console.log(`Mock server on port ${server.port}`)

// Stop the server
server.close()

Useful for integration tests:

import { describe, it, beforeAll, afterAll } from 'vitest'
import { createCliServer } from '@schmock/cli'

let server: Awaited<ReturnType<typeof createCliServer>>

beforeAll(async () => {
  server = await createCliServer({
    spec: './api.yaml',
    port: 0,  // random available port
    seed: './fixtures/seed.json',
  })
})

afterAll(() => server.close())

it('serves the API', async () => {
  const res = await fetch(`http://127.0.0.1:${server.port}/users`)
  expect(res.status).toBe(200)
})