API Mock Generator
Generate lightweight mock request and response definitions for frontend testing, demos and API docs.
Back to all tools on ToolForge
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:
- Request Definition: HTTP method and endpoint URL
- 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
- Use realistic data: Valid email formats, proper phone numbers, real-looking names
- Follow schemas: Match the expected API contract - field names, types, nesting
- Include edge cases: Empty arrays, null values, max-length strings
- ISO 8601 dates: Use "2025-01-15T10:30:00Z" format for timestamps
- Consistent IDs: Use integers or UUIDs consistently across mocks
- Error messages: Include user-friendly error messages for 4xx/5xx responses
Common Use Cases
- Frontend Development: Build UIs before backend APIs are ready
- Unit Testing: Provide predictable responses for component tests
- Integration Testing: Test API client behavior with various status codes
- API Documentation: Show example request/response pairs
- Prototyping: Demo applications without live backend
- Training: Create safe environments for learning APIs
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
- Select HTTP method: Choose GET, POST, PUT, PATCH, or DELETE.
- Enter endpoint URL: Type the API path (e.g., /api/users).
- Set status code: Enter the response status (200, 404, 500, etc.).
- Add headers: Enter response headers in Key: Value format, one per line.
- Enter response body: Paste or type JSON for the response payload.
- Click Generate Mock: JavaScript code with the mock object appears.
- Copy mock: Use the generated code in your tests or application.
Tips
- Use valid JSON in the body field - invalid JSON shows an error
- Headers are optional - leave empty for minimal mocks
- Generate multiple mocks for different scenarios (success, error, empty)
- Match your mock structure to the actual API schema
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.