API Mock Generator

Generate lightweight mock request and response definitions for frontend testing, demos and API docs.

Back to all tools on ToolForge

More in JSON & API









Mock Output

About API Mock Generator

This API mock generator creates lightweight mock request and response objects for testing and documentation. It generates JavaScript code that can be used directly in frontend applications, test suites, or API documentation.

Mock Response Structure

The generated mock contains two main parts:

  1. Request Definition: HTTP method and endpoint URL
  2. Response Definition: Status code, headers, and body
Generated Mock Example:

const mock = {
  request: {
    method: "GET",
    url: "/api/users"
  },
  response: {
    status: 200,
    headers: {
      "Content-Type": "application/json"
    },
    body: {
      "success": true,
      "data": {
        "id": 1,
        "name": "John Doe",
        "email": "[email protected]"
      }
    }
  }
};

return new Response(JSON.stringify(mock.response.body), {
  status: mock.response.status,
  headers: mock.response.headers
});

Common HTTP Status Codes for Mocks

Status Code Meaning Use Case
200 OK Successful GET, PUT, PATCH
201 Created Successful resource creation (POST)
204 No Content Successful DELETE (empty body)
400 Bad Request Invalid input, validation errors
401 Unauthorized Missing or invalid authentication
403 Forbidden Insufficient permissions
404 Not Found Resource doesn't exist
500 Server Error Simulate backend failures

Common Response Headers

Header Example Value Purpose
Content-Type application/json Response body format
Cache-Control no-store Prevent caching
X-RateLimit-Limit 100 Rate limit info
X-Request-ID abc-123-xyz Request tracking
Set-Cookie session=abc123; HttpOnly Session management

Mock Data Best Practices

Common Use Cases

Integration with Testing Libraries

// Jest + MSW (Mock Service Worker)
import { rest } from 'msw';

rest.get('/api/users', (req, res, ctx) => {
  return res(
    ctx.status(200),
    ctx.json({
      success: true,
      data: { id: 1, name: 'John Doe' }
    })
  );
});

// Cypress
cy.intercept('GET', '/api/users', {
  statusCode: 200,
  body: { success: true, data: {...} }
});

// Fetch API mock
const mockResponse = new Response(
  JSON.stringify({ success: true }),
  { status: 200, headers: { 'Content-Type': 'application/json' } }
);

How to Generate Mock API Responses

  1. Select HTTP method: Choose GET, POST, PUT, PATCH, or DELETE.
  2. Enter endpoint URL: Type the API path (e.g., /api/users).
  3. Set status code: Enter the response status (200, 404, 500, etc.).
  4. Add headers: Enter response headers in Key: Value format, one per line.
  5. Enter response body: Paste or type JSON for the response payload.
  6. Click Generate Mock: JavaScript code with the mock object appears.
  7. Copy mock: Use the generated code in your tests or application.

Tips

Frequently Asked Questions

What is a mock API response?
A mock API response is a simulated server response that mimics the structure and behavior of a real API. It contains a request definition (method, URL) and response definition (status code, headers, body). Mocks allow frontend development to proceed without a live backend.
Why use mock API responses in development?
Mocks enable parallel development - frontend teams can build UIs while backend APIs are still in development. They also provide consistent, predictable responses for testing edge cases (errors, empty states) that are hard to reproduce with live APIs.
What should a good mock response include?
A good mock includes: realistic status codes (200, 400, 404, 500), appropriate headers (Content-Type, Cache-Control), and a body matching the expected API schema. Use realistic sample data - actual names, valid email formats, proper date formats (ISO 8601).
How do I mock different HTTP status codes?
Set the status code field to simulate different scenarios: 200 for success, 201 for resource created, 400 for bad request, 401 for unauthorized, 404 for not found, 500 for server error. Adjust the response body to match - error messages for 4xx/5xx codes.
What are common mock API use cases?
Common uses: frontend development before backend is ready, unit/integration testing with predictable responses, API documentation with example payloads, prototyping and demos, load testing with simulated data, and training environments without production access.
How do I integrate mocks into my test suite?
Copy the generated mock object into your test fixtures. In Jest, use jest.mock() or MSW (Mock Service Worker) to intercept requests. In Cypress, use cy.intercept(). For unit tests, pass the mock directly to components or services that expect API responses.