Mock server migration guide
Updated November 2026: The old mock platform is gone. This guide now covers mock servers last published before November 9, 2024, which must be republished to work again. For everything else, see How code-based mocks work.
Introduction
On November 9, 2024, APIGIT launched a new MockServer backend, powered by Express.js and running in a Node.js sandbox environment. The old platform, at *.mock.apigit.com, has since been retired.
If your mock server was last published before that date, it is not reachable until you republish it. If you were sent here by a notice on your mock server's page, this guide is for you. Moving from the old version (pure JavaScript-based) to the new one involves some breaking changes.
What You Need to Do
- Check your plan. These are code-based mock servers, and republishing one needs a Team or Enterprise plan.
- Update your scripts in the
apigit-mock/folder, as described below.apigit-mock/main.jsmust exist. - Open the mock server from the
Publish Centerand clickRepublish, which rebuilds it from the same commit and name. If you changed your scripts, overwrite the mock server from your new commit instead. - Update your clients to the new URL.
On the Free plan? You can't republish a code-based mock server, but you can publish a new spec-based mock server from your OpenAPI document: it needs no scripts and works on every plan. To keep the responses your scripts returned, add them to the document as example values. See Migrating from a code-based mock.
Key Changes and Migration Details
1. URL Change for Mock Servers
Old URL format: your-mockserver.mock.apigit.com
New URL format: your-mockserver.mockserver.apigit.com
The old URL no longer answers. A republished mock server keeps its name, under the new host.
Example:
- Old URL:
https://your-mockserver.mock.apigit.com/endpoint - New URL:
https://your-mockserver.mockserver.apigit.com/endpoint
Be sure to update any references in your client applications, documentation, and integrations.
2. Library Imports - No More Global Libraries Variables
In the old MockServer, several popular JavaScript libraries were globally available, allowing direct usage without explicit imports. These included:
- lodash (
_) (v4.17.21) - General utility functions - momentjs (
moment) (v2.30.1) - Time and date handling - faker (
faker) (v5.5.3) - Data generator - ajv (
Ajv) (v6.10.0) - JSON schema validation - validator (
validator) (v13.12.0) - Validation helper
In the new version of MockServer, these libraries must now be manually imported using require statements in your mock scripts before use. An import statement does not find them.
What You Need to Do:
- Add appropriate
requirestatements for any supported libraries used in your mock scripts.
Supported Libraries in the New MockServer
The new MockServer comes with popular JavaScript libraries pre-installed. It has no network access, so a library must be on this list or included in your apigit-mock/ folder. Below is a list of supported libraries and examples of how to use them:
- @faker-js/faker (v9.2.0) - Data generator
- ajv (v8.17.1) - JSON schema validation
- base64-js (v1.5.1) - Base64 encoding/decoding
- body-parser (v1.20.3) - Request body parsing
- cookie-parser (v1.4.7) - Cookie parsing
- cors (v2.8.5) - Cross-Origin Resource Sharing middleware
- jsonwebtoken (v9.0.2) - JWT creation and verification
- lodash (v4.17.21) - General utility
- moment (v2.30.1) - Time and date handling
- uuid (v11.0.3) - UUID generator
- validator (v13.12.0) - Validation helper
Example Usage:
var moment = require('moment'); // require
const { faker } = require('@faker-js/faker');
const Ajv = require("ajv");
var validator = require('validator');
const base64 = require('base64-js');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
const _ = require('lodash');
function testLodash() {
// Test data
const users = [
{ id: 1, name: 'Alice', age: 25, active: true },
{ id: 2, name: 'Bob', age: 30, active: false },
{ id: 3, name: 'Charlie', age: 35, active: true },
{ id: 4, name: 'David', age: 40, active: false }
];
// Example 1: Filter active users
const activeUsers = _.filter(users, { active: true });
console.log('Active Users:', activeUsers);
// Example 2: Sort users by age in descending order
const sortedByAge = _.orderBy(users, ['age'], ['desc']);
console.log('Users sorted by age (desc):', sortedByAge);
// Example 3: Get the names of all users
const userNames = _.map(users, 'name');
console.log('User Names:', userNames);
}
function testMoment() {
console.log("Current time is:", moment().format());
}
function testUuidv4() {
console.log("UUID is:", uuidv4());
}
function testAjv() {
const ajv = new Ajv();
const schema = {
type: "object",
properties: {
foo: { type: "integer" },
bar: { type: "string" },
},
required: ["foo"],
additionalProperties: false,
};
const data = { foo: 1, bar: "abc" };
const validate = ajv.compile(schema);
const valid = validate(data);
if (!valid) console.log(validate.errors);
}
function testBase64() {
const text = 'Hello, world!';
const textBytes = new TextEncoder().encode(text);
const base64Encoded = base64.fromByteArray(textBytes);
console.log('Base64 Encoded:', base64Encoded);
const decodedBytes = base64.toByteArray(base64Encoded);
const decodedText = new TextDecoder().decode(decodedBytes);
console.log('Decoded Text:', decodedText);
}
function testValidator() {
const result = validator.isEmail('fooo~bar.com');
console.log("fooo~bar.com is email, right?", result);
}
function testFakerJS() {
return {
userId: faker.string.uuid(),
username: faker.internet.username(),
email: faker.internet.email(),
avatar: faker.image.avatar(),
password: faker.internet.password(),
birthdate: faker.date.birthdate(),
registeredAt: faker.date.past(),
};
}
function testJwt() {
const SECRET_KEY = 'your-secret-key';
const payload = { userId: 123, username: 'testUser' };
const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '1h' });
console.log('Generated Token:', token);
try {
const decoded = jwt.verify(token, SECRET_KEY);
console.log('Decoded Payload:', decoded);
} catch (err) {
console.error('Token verification failed:', err.message);
}
}
mock.get("/testlibs", function (req, res) {
console.log("==== this is a test of libs supported =====");
testLodash();
testMoment();
testUuidv4();
testAjv();
testValidator();
testBase64();
testJwt();
const jsonData = testFakerJS();
return res.status(200).json(jsonData);
});
3. Routes: mock.define() and mock.[get|post|delete|...]()
mock is now an express.js app, so you can define routes the native way with mock.[get|post|delete|...]().
mock.define(path, method, callback) still works, accepts {name} path parameters as well as :name, and is the way to have a route listed by GET /apigit-mockserver-routes.
What You Need to Do:
Nothing is required for existing mock.define() routes. See Routes and Middleware.