API and code · Mock server docs

Spectrail mock server

The mock server answers like your real API, so your end-to-end tests run before the backend exists.

The mock server is part of every plan except Time only. Request access · See the plans

The mock server answers your app's API calls the way your endpoints are documented in Spectrail. Point your app or your end-to-end tests at it instead of a mock you keep yourself.

  • Every endpoint of the project answers at https://mock.spectrail.app/<key>/<path>.
  • Without a session it is stateless: the same call gets the same answer every time.
  • In a session it keeps state: log in, pick a scenario, change state through actions, confirm two-step approvals, force errors and read a request log. Each test run gets its own session.

You find the key on the Endpoints page: Mock server → Base address (https://mock.spectrail.app/mk_…). Every member can read it; an owner or admin can make a new one (the old key stops at once and every session of the project ends). The mock server is part of the plans that include it.

The pages

PageWhat
Endpoint mock blockThe mock block of an endpoint: every key with examples
Project configThe project's mock config: login, scenarios, confirm codes, the 401 answer
Sessions and control callsSessions and every /__mock control call
Two-step approvalsTwo-step approvals: token, id or request mode; confirm; refused actions
State, rules and templatesState, sets (list selectors, remove, logout), templates, when and match
Test helpersForced answers, the request log, delay, multipart, accept rules
Editing with MCPHow your AI agent edits all of this through MCP
Full exampleA full example (deactivate a cash account, two approval paths, depots) to paste

URL forms

URLWhat
https://mock.spectrail.app/<key>/<path>An endpoint call. In a session only with the X-Mock-Session header or a token the mock issued.
https://mock.spectrail.app/<key>/__mock/s/<session>/<path>Session URL: the call runs in that session, with or without a token. For apps that cannot send extra headers (a mobile UI test tool, a phone build).
https://mock.spectrail.app/<key>/__mock/<call>A control call (sessions, reset, scenario, state, force, log).
https://mock.spectrail.app/<key>/__mock/s/<session>/__mock/<call>A control call for that session.

Set your app's API base address to the session URL (…/<key>/__mock/s/<session>) and every call of the app runs in that session.

Which session a call uses: the session URL first, then the X-Mock-Session header, then the session of the login token in Authorization. An unknown session answers 404 {"error":"unknown_session"}.

Headers

HeaderDirectionWhat
X-Mock-Session: <session>requestRun the call in that session.
X-Mock-Scenario: <name>requestThe scenario of a session made by POST /__mock/sessions (when the body names none) or by a login without a session.
X-Mock-Delay: <ms>requestIn a session: wait this long before answering (≤ 10,000).
Spectrail-Status: <status> or ?__status=<status>requestAnswer with this documented status instead of the default.
Authorization: <scheme> <token>requestAny scheme word works with a token the mock issued (Token mt_…, Bearer mt_…).
Spectrail-Endpoint, Spectrail-RevanswerThe endpoint id and revision that answered.
Deprecation: trueanswerThe endpoint is deprecated.
Retry-AfteranswerOn 429.

CORS is open (any origin, any header). Answers are never cached. Cookies are never read or set.

How a call is matched

Method and path, as documented. A {placeholder} matches one path segment. A trailing slash does not matter. When two endpoints match, the one with fewer placeholders wins. No match: 404 {"error":"No endpoint GET /x in this project's mock"}.

The answer's status is the lowest documented 2xx (else the lowest documented status), unless you ask for another one with Spectrail-Status / ?__status=.

What one call does

In this order:

  1. A forced answer queued for this endpoint in this session answers first (Test helpers).
  2. Login check: with a mock login set up, an endpoint with auth required needs a token the mock issued (of this session); else the configured 401 answer.
  3. Request checks: auth required without an Authorization header → 401; a missing required query or body field → 400 {"error":"Missing required fields","fields":["body.b"]}.
  4. Accept rules on field values (Test helpers).
  5. Confirm: a confirm endpoint needs the pending approval's reference (and a code) (Two-step approvals).
  6. Preset: the scenario's presets first, then the endpoint's own; match reads the request, when the state. No preset → fake values built from the documented response fields (examples win).
  7. After a 2xx that is not final: an approval endpoint stores its pending approval; else the endpoint's sets change the state.
  8. One save of the session, one log row, then the delay.

Without a session: no forced answers, approvals, state changes, log or X-Mock-Delay. Accept rules, match and final still apply; {{state.…}} templates give null.

Request bodies are read the same way whether they are JSON or application/x-www-form-urlencoded (form values are strings). Multipart bodies are accepted; only the file's name, type and size are kept.

One call at a time per session

A session takes one call at a time. Two calls in the same session at the same moment can lose a state change. Run a test's flow one call after another; run parallel tests in different sessions.

Limits

WhatLimit
Calls600 per minute per key, 1,200 per minute per client IP (then 429, Retry-After: 60)
Request body64 KB; multipart 4 MB (files are never kept)
Sessions500 open per key; a session ends after 24 hours without a call
Session state256 KB (a call whose sets would grow it more answers 500 mock_state_too_large, nothing saved)
Template answer1 MB of filled-in values (else 500 mock_answer_too_large)
Pending approvalsone per session (a new one replaces the old one)
Request log500 rows per session, about 20,000 per project, body cut at 16 KB, kept 24 hours
Forced answers50 per session (120 KB), times 1..100, status 200..599
Delay10 seconds per call
mock block128 KB per endpoint (limits per key)
Project mock config512 KB; 100 scenarios, 50 login users, 20 confirm codes

Not part of the mock server

  • Pass-through to a real server: the mock never calls other addresses. Route calls the mock does not know in your own test setup.
  • WebSocket.
  • Maths or code in rules: derived values (a balance, hasOpenDepot) are set explicitly by each rule.
  • A web screen for the mock settings: the Endpoints page shows a block read-only; you edit through MCP (Editing with MCP) for now.

The mock block and the mock config never appear in share links, project exports or Spectrail AI answers. A revision that changes only an endpoint's mock block is kept in its history but stays out of the contract signals (Live, the dashboard, AI spotted, retro).