Skip to Content
Mock ServerHow code-based mocks work

How code-based mocks work

A code-based mock server runs your own JavaScript. APIGit creates an Express.js app for you, loads apigit-mock/main.js from the published commit, and serves it at your mock server’s URL. Use it when responses must depend on earlier requests or on real logic. It needs a Team or Enterprise plan.

apigit-mock/main.js
mock.get('/users/:name', (req, res) => { res.json({ name: req.params.name, createdAt: new Date().toISOString() }); }); mock.post('/users', (req, res) => { state.users = state.users || []; state.users.push(req.body); res.status(201).json({ count: state.users.length }); });

What you get

  • Express.js 4, without the setup. mock is the app: use mock.get(), mock.post(), mock.use() and everything else in the Express API .
  • Parsed requests. JSON, URL-encoded and text bodies, and cookies, are parsed before your code runs, and CORS is enabled.
  • Libraries such as faker, lodash and jsonwebtoken, ready to require.
  • State that persists between requests.
  • Modules: split your code into files under apigit-mock/.
  • Logs: whatever your code writes with console.log appears with the request in the mock’s logs.

Start from your API document

You do not have to write the routes by hand. Open the repository’s Mock Scripts tab and click Generate from Spec to generate routes from an API document, then edit the code. The generated code is yours from then on and does not change when the document does.

Generate mock routes from an API document

If you do not need custom logic, a spec-based mock needs no generated code at all and always matches its document.

Built-in routes

RouteReturns
GET /apigit-mockserver-routesThe routes defined with mock.define()
GET /apigit-mockserver-stateThe current state

Security

Your mock’s code runs in a sandbox with its own isolated execution environment, separate from every other mock server. It has no network access, so it cannot call outside services, and it holds no cloud credentials.

Limits

LimitValue
apigit-mock/ folder2 MiB zipped
State2 MB
Requests per second, per mock server60
⚠️

Avoid the routes /ping and /sping: the platform answers them itself, so they never reach your mock. Use a name such as /health for your own check.

Errors

StatusAnswerMeaning
404route not foundNo route matches the request
404mock server main.js not foundThe published commit has no apigit-mock/main.js
500Loading main.js failedmain.js threw an error when loading. The error field has the message.
500Internal Server Error (critical)A route threw an error. The error field has the message.

Migrating from a code-based mock

Moving a code-based mock to spec-based means publishing a new mock server, because a mock server keeps its type. Add example values to your OpenAPI document for the responses your scripts returned, then publish a spec-based mock from it. Each mock has its own URL, so run both while clients move.

Last updated on