Mock API

A simulated API endpoint that returns predefined responses for testing and development.

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 vs Real API

AspectMock APIReal API
DataPredefined/GeneratedLive database
AvailabilityAlways availableMay have downtime
SpeedInstant responsesNetwork latency
CostFreeMay 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

  1. JSON Files: Simple static responses
  2. Mock Servers: Tools like JSON Server, Mockoon
  3. Cloud Services: Mockable.io, Beeceptor, MockAPI
  4. 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 }
  ]
}