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.
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.
mockis the app: usemock.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,lodashandjsonwebtoken, ready torequire. - State that persists between requests.
- Modules: split your code into files under
apigit-mock/. - Logs: whatever your code writes with
console.logappears 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.

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
| Route | Returns |
|---|---|
GET /apigit-mockserver-routes | The routes defined with mock.define() |
GET /apigit-mockserver-state | The 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
| Limit | Value |
|---|---|
apigit-mock/ folder | 2 MiB zipped |
| State | 2 MB |
| Requests per second, per mock server | 60 |
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
| Status | Answer | Meaning |
|---|---|---|
| 404 | route not found | No route matches the request |
| 404 | mock server main.js not found | The published commit has no apigit-mock/main.js |
| 500 | Loading main.js failed | main.js threw an error when loading. The error field has the message. |
| 500 | Internal 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.