APIGit
Native Git RepositoryVersioned, Git-native API repositoryAPI DesignOpenAPI, AsyncAPI and GraphQL editingAPI DocumentationPublish versioned docs from GitAPI GovernanceCheck OpenAPI and AsyncAPI standardsMock ServerFrom OpenAPI, GraphQL or Express.js codeTeam CollaborationOrganizations, teams, permissions and reviewsAI AgentsMCP server, llms.txt and Copy page for agentsCLILocal-first: lint, mock and publish from a terminal

The full API lifecycle

Design, document, mock and govern your APIs in one Git-native workspace, or local-first with the CLI and your coding agent.

Get started freeView pricing

Learning

Help DocsGetting StartedVideo TutorialsBlog

Support

Contact UsRequest a DemoReport an Issue
PricingAbout
Sign InSign Up for Free
APIGit

The Git-native, local-first API platform, ready for AI agents

Product

  • Native Git Repository
  • API Design
  • API Documentation
  • API Governance
  • Mock Server
  • Team Collaboration
  • AI Agents
  • CLI

Resources

  • Blog
  • Documentation
  • CLI Reference
  • MCP Server
  • Video Tutorials
  • Pricing
  • Contact

Company

  • About
  • Careers
  • Privacy Policy
  • Terms of Service
  • Service Level Agreement
  • Cookie Policy

© 2026 APIGit. All rights reserved.

Home / Blog / mock-server / explorer-mock-server

Explore the Mock Server in APIGit

2023-04-28
APIGit Team
5 min read
mock-server

Generate Mock Server script automatically

This post is about code-based mock servers, available on Team and Enterprise plans. If you only need answers from your OpenAPI document, a spec-based mock server needs no script at all.

If you have created your own specification within your repository, congratulations! You can now automatically generate the Mock Server script. To do so, simply navigate to "Mock Scripts" within your repository, enter edit mode and click on "Generate from Spec".

Generate Mock Server script automatically action Generate Mock Server script from API spec

Select an API specification and an operation, and the route script is generated for you.

Generate Mock Server script automatically result Mock Server script according to API spec

How simple is the mock server script?

Thanks to the powerful automatic Mock Server tool, you will receive several scripts related to the API specification. However, it is important to note that only a few lines of code are required to deploy the Mock Server.

//this script should be in apigit-mock/main.js
mock.define("/", "GET", function (req, res) {
    res.status(200).send("welcome to apigit mock server");
}); 

Within the "main.js" file, you will find a variety of route functions that are related to the API specification. You are free to define additional functions within this file for the purpose of further testing.

What is Dynamic MockServer?

With Dynamic MockServer, developers can create multiple scenarios for different endpoints, each with different response data, headers, and status codes. This allows developers to test their applications in various scenarios and conditions, ensuring that they work as expected in different situations.

Deploying a Dynamic Mock Server on the Apigit platform is a straightforward process. By defining an array list and adding, updating, or searching within it, you can receive responses based on the requests that you have sent previously.

//all routes should be defined in main.js
const { v4: uuidv4 } = require("uuid");

// create a user
mock.define("/user", "POST", function (req, res) {
    var user = req.body;
	if (user.username === "") {
		res.status(405).send("Invalid input");
		return;
	}
	user.id = uuidv4(); // generate uniqueID for the new user
	state.users = state.users || []; // global state
	state.users.push(user);

	res.status(200).send(user);
});

// find user by name
mock.define("/user/{username}", "GET", function (req, res) {
    var users = state.users || [];
    var user = null;
	for (var i = 0; i < users.length; i++) {
		if (users[i].username == req.params.username) {
			user = users[i];
			break;
		}
	}
	if (user === null) {
		res.status(404).send("User not found");
		return;
	}
	res.status(200).send(user);
});

What a software engineer would like?

To ensure that your script remains organized and easy to understand, it is recommended that you define all routes within the "main.js" file. However, if you prefer to keep your code more modular, you can create a new route file by clicking on the "+" icon and adding your callback function to the new file. The file structure will appear as follows:

//main.js
var pet = require("./routes/pet.js")
// define the routes callback in seperated files
// pet
mock.define("/pet", "PUT", pet.updatePet); // update an existing pet

And define the callback function in another file.

//./routes/pet.js
exports.updatePet = function (req, res) {
    var pets = state.pets || [];
    var pet = req.body;
    var i = 0;

    for (i = 0; i < pets.length; i++) {
        if (pets[i].id == pet.id) {
            pets[i] = pet;
            state.pets = pets;
            break;
        }
    }

    if (i === pets.length) { // menas not found the pet
        res.status(404).send('Pet not found');
        return;
    }

    res.status(200).send(pet);
};

For full example, you can visit Github.

Keep exploring

To use the Mock Server that you have created, you must first publish it: click Publish, choose Publish MockServer and select Code-based. Once it has been published, you can find the server address in the repository's Publish Center. You can keep exploring the mock server script by visiting Automatic generation of Ready-to-Use Mockservers or Mock Server API Definition.

Back to Blog

Related Posts

Spec-Based Mock Servers: A Working Mock from Your OpenAPI Document

APIGit • 2026-11-06
mock-server

Publish a mock server from your OpenAPI documents without writing code. It validates requests, answers from your examples and schemas, and works on every plan.

Read More

Spec-Based or Code-Based: Which Mock Server Do You Need?

APIGit • 2026-11-06
mock-server

APIGit has two types of mock server. Here is how they differ, when each one fits, and how to run both from one repository.

Read More

Mock Servers That Tell You What Is Wrong

APIGit • 2026-11-06
mock-server

Spec-based mock servers validate each request against your OpenAPI document, return clear problem+json errors, and let clients pick a response with the Prefer header.

Read More

Ready to get started with APIGit?

Create a free workspace, add an OpenAPI file and publish your first API document and mock server in minutes.

Get Started Book a Demo