API mocking is the practice of creating simulated versions of APIs that mimic the behavior of real services. It enables teams to develop and test applications independently of actual backend systems.
API Mocking
Simulating API behavior to enable development and testing without real backend services.
Why Mock APIs?
- Faster Development: No waiting for backend teams to finish
- Reliable Testing: Consistent responses without network issues
- Cost Reduction: Avoid third-party API charges during development
- Edge Case Testing: Easily simulate errors and unusual scenarios
Mocking Approaches
| Approach | Description | Best For |
|---|---|---|
| Static Mocks | Fixed JSON responses | Simple testing |
| Dynamic Mocks | Generated based on request | Realistic scenarios |
| Record & Replay | Capture real responses | Integration testing |
| Contract-Based | From OpenAPI/Swagger specs | API-first development |
Popular Tools
- MSW - Mock Service Worker for browser/Node
- Nock - HTTP mocking for Node.js
- Mirage JS - Client-side API mocking
- Prism - OpenAPI-based mock server
Code Examples
MSW Handler
import { http, HttpResponse } from "msw";
export const handlers = [
http.get("/api/users", () => {
return HttpResponse.json([
{ id: 1, name: "John" },
{ id: 2, name: "Jane" }
]);
})
];Related Terms
OpenAPI Specification
A standard format for describing REST APIs, enabling documentation and code generation.
Read more
WebSocket
A protocol enabling full-duplex, real-time communication between client and server.
Read more
Latency
The time delay between a request being sent and a response being received.
Read more
Server-Sent Events (SSE)
A standard for pushing real-time updates from server to client over HTTP.
Read more