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
| Page | What |
|---|---|
| Endpoint mock block | The mock block of an endpoint: every key with examples |
| Project config | The project's mock config: login, scenarios, confirm codes, the 401 answer |
| Sessions and control calls | Sessions and every /__mock control call |
| Two-step approvals | Two-step approvals: token, id or request mode; confirm; refused actions |
| State, rules and templates | State, sets (list selectors, remove, logout), templates, when and match |
| Test helpers | Forced answers, the request log, delay, multipart, accept rules |
| Editing with MCP | How your AI agent edits all of this through MCP |
| Full example | A full example (deactivate a cash account, two approval paths, depots) to paste |
URL forms
| URL | What |
|---|---|
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
| Header | Direction | What |
|---|---|---|
X-Mock-Session: <session> | request | Run the call in that session. |
X-Mock-Scenario: <name> | request | The 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> | request | In a session: wait this long before answering (≤ 10,000). |
Spectrail-Status: <status> or ?__status=<status> | request | Answer with this documented status instead of the default. |
Authorization: <scheme> <token> | request | Any scheme word works with a token the mock issued (Token mt_…, Bearer mt_…). |
Spectrail-Endpoint, Spectrail-Rev | answer | The endpoint id and revision that answered. |
Deprecation: true | answer | The endpoint is deprecated. |
Retry-After | answer | On 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:
- A forced answer queued for this endpoint in this session answers first (Test helpers).
- Login check: with a mock login set up, an endpoint with auth
requiredneeds a token the mock issued (of this session); else the configured 401 answer. - Request checks: auth
requiredwithout anAuthorizationheader → 401; a missing required query or body field → 400{"error":"Missing required fields","fields":["body.b"]}. - Accept rules on field values (Test helpers).
- Confirm: a confirm endpoint needs the pending approval's reference (and a code) (Two-step approvals).
- Preset: the scenario's presets first, then the endpoint's own;
matchreads the request,whenthe state. No preset → fake values built from the documented response fields (examples win). - After a 2xx that is not
final: an approval endpoint stores its pending approval; else the endpoint'ssetschange the state. - 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
| What | Limit |
|---|---|
| Calls | 600 per minute per key, 1,200 per minute per client IP (then 429, Retry-After: 60) |
| Request body | 64 KB; multipart 4 MB (files are never kept) |
| Sessions | 500 open per key; a session ends after 24 hours without a call |
| Session state | 256 KB (a call whose sets would grow it more answers 500 mock_state_too_large, nothing saved) |
| Template answer | 1 MB of filled-in values (else 500 mock_answer_too_large) |
| Pending approvals | one per session (a new one replaces the old one) |
| Request log | 500 rows per session, about 20,000 per project, body cut at 16 KB, kept 24 hours |
| Forced answers | 50 per session (120 KB), times 1..100, status 200..599 |
| Delay | 10 seconds per call |
mock block | 128 KB per endpoint (limits per key) |
| Project mock config | 512 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).