Last updated
Why Use the API Mock Server Generator
- Multiple tools — JSON Server, Express.js, Mockoon, and WireMock configurations
- Error scenarios — 400, 401, 403, 404, 429, 500 responses for testing error handling
- Delayed responses — simulate slow APIs for timeout testing
- Dynamic responses — path parameters reflected in response data
- Postman collections — ready-to-import test collections generated alongside the mock
- Frontend independence — frontend development proceeds without waiting for backend
Use the API Mock Server generator at TechConverter.me to set up realistic mock APIs quickly, enabling parallel development and reliable testing without depending on real external services.
Examples
Example 1: JSON Server Configuration
The simplest mock server — define your data as JSON and get a full REST API automatically:
// db.json — paste this into JSON Server
{
"users": [
{ "id": 1, "name": "Alice Johnson", "email": "alice@example.com", "role": "admin" },
{ "id": 2, "name": "Bob Smith", "email": "bob@example.com", "role": "user" },
{ "id": 3, "name": "Carol White", "email": "carol@example.com", "role": "user" }
],
"products": [
{ "id": 1, "name": "Widget Pro", "price": 29.99, "stock": 150 },
{ "id": 2, "name": "Gadget Plus", "price": 49.99, "stock": 75 }
],
"orders": [
{ "id": 1, "userId": 1, "productId": 2, "quantity": 2, "status": "shipped" }
]
}
# Run with:
npx json-server --watch db.json --port 3001
# Automatically available endpoints:
GET /users → returns all users
GET /users/1 → returns user with id 1
POST /users → creates a new user
PUT /users/1 → replaces user 1
PATCH /users/1 → updates user 1 fields
DELETE /users/1 → deletes user 1
GET /users?role=admin → filters by role
Example 2: Express.js Mock Server
// mock-server.js — generated Express mock server
const express = require('express');
const app = express();
app.use(express.json());
// GET /users — list with pagination
app.get('/users', (req, res) => {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 20;
res.json({
data: [
{ id: 1, name: "Alice Johnson", email: "alice@example.com" },
{ id: 2, name: "Bob Smith", email: "bob@example.com" }
],
pagination: { page, limit, total: 2, totalPages: 1 }
});
});
// GET /users/:id — single user or 404
app.get('/users/:id', (req, res) => {
if (req.params.id === '999') {
return res.status(404).json({
error: 'NOT_FOUND',
message: 'User not found'
});
}
res.json({ id: parseInt(req.params.id), name: "Alice Johnson" });
});
// POST /users — create with validation error simulation
app.post('/users', (req, res) => {
if (!req.body.email) {
return res.status(422).json({
error: 'VALIDATION_ERROR',
fields: { email: 'Email is required' }
});
}
res.status(201).json({ id: 3, ...req.body, createdAt: new Date() });
});
// Simulate 500 error for testing
app.get('/error', (req, res) => {
res.status(500).json({ error: 'INTERNAL_ERROR', message: 'Server error' });
});
app.listen(3001, () => console.log('Mock server running on port 3001'));
Example 3: Response Scenarios for Testing
// Mock server with multiple response scenarios
const scenarios = {
success: { status: 200, body: { id: 1, name: "Alice" } },
notFound: { status: 404, body: { error: "NOT_FOUND" } },
unauthorized: { status: 401, body: { error: "UNAUTHORIZED" } },
rateLimited: { status: 429, body: { error: "RATE_LIMIT_EXCEEDED" },
headers: { 'Retry-After': '60' } },
serverError: { status: 500, body: { error: "INTERNAL_ERROR" } },
delayed: { status: 200, body: { id: 1 }, delay: 3000 } // 3 second delay
};
// Switch scenarios via query param: GET /users/1?scenario=notFound
app.get('/users/:id', (req, res) => {
const scenario = scenarios[req.query.scenario] || scenarios.success;
if (scenario.delay) {
setTimeout(() => res.status(scenario.status).json(scenario.body),
scenario.delay);
} else {
res.status(scenario.status).json(scenario.body);
}
});