A Mock API is a fake API endpoint that simulates the behavior of a real API. It returns predefined or dynamically generated responses, allowing developers to work without depending on actual backend services.
Mock API
A simulated API endpoint that returns predefined responses for testing and development.
Mock API vs Real API
| Aspect | Mock API | Real API |
|---|---|---|
| Data | Predefined/Generated | Live database |
| Availability | Always available | May have downtime |
| Speed | Instant responses | Network latency |
| Cost | Free | May have usage fees |
Common Use Cases
- Frontend Development: Build UI before backend is ready
- Unit Testing: Isolate components from external dependencies
- Demo & Prototyping: Show functionality without real data
- CI/CD Pipelines: Run tests without external services
Creating Mock APIs
- JSON Files: Simple static responses
- Mock Servers: Tools like JSON Server, Mockoon
- Cloud Services: Mockable.io, Beeceptor, MockAPI
- Code-Based: MSW, Nock, WireMock
Code Examples
JSON Server db.json
{
"users": [
{ "id": 1, "name": "John Doe", "email": "john@example.com" },
{ "id": 2, "name": "Jane Smith", "email": "jane@example.com" }
],
"posts": [
{ "id": 1, "title": "Hello World", "userId": 1 }
]
}Related Terms
REST API
An architectural style for building web APIs using HTTP methods and stateless communication.
Read more
OpenAPI Specification
A standard format for describing REST APIs, enabling documentation and code generation.
Read more
Latency
The time delay between a request being sent and a response being received.
Read more
HTTP Status Codes
Standard response codes that indicate the result of an HTTP request.
Read more