Prompt
How do I mock a downstream API for local development?
Latest observation
A good way to mock a downstream API for local development depends on how much realism you need and what stack you’re using. Common options:
1) Run a local mock server
Create a small HTTP server that returns canned responses matching the real API.
Good for: fast local dev, reproducible responses
Tools: JSON Server, WireMock, Mockoon, Prism, json-server, Express/FastAPI/Flask, etc.
Example (Node/Express):
import express from "express";
const app = express();
app.get("/users/:id", (req, res) => {
res.json({ id: req.params.id, name: "Mock User" });
});
app.listen(3001, () => console.log("Mock API on http://localhost:3001"));
Then point your app’s base URL to http://localhost:3001.
2) Use service virtualization tools
These are designed for HTTP mocks, contract testing, and replaying recorded traffic.
- WireMock: very common for REST APIs
- Mockoon: easy GUI-based mock server
- Prism: can mock from OpenAPI specs
- Stoplight: API design + mocking
Good for: teams, complex APIs, OpenAPI-driven workflows
3) Mock at the client layer
If your app calls the downstream API through a wrapper/client, mock that client in tests or dev builds.
Good for: unit/integration tests, keeping local app startup simple
Example approach: replace the API client implementation with a fake one behind an interface.
4) Proxy and record/replay
Run a proxy that records real API responses and replays them later.
Good for: realistic responses, less manual setup
Caution: keep secrets out, and be careful with stale/production-specific data.
5) Use a dev/staging environment
If allowed, point local dev to a sandbox/staging API.
Good for: real behavior without building mocks
Tradeoff: external dependency, slower, may be unstable or rate-limited.
Recommended approach
For most local development, use:
- Mock server with canned responses for simplicity
- OpenAPI-based mocking if you already have an API spec
- Client abstraction so you can switch between real and mock via config
Practical setup pattern
- Put the downstream base URL in config:
DOWNSTREAM_API_BASE_URL=http://localhost:3001
- Run a mock server locally
- Add sample fixtures for common responses
- Optionally add error cases:
- 404, 500, timeouts, malformed payloads
- Use environment variables or a feature flag to switch between real and mocked APIs
Example config switch
# real API
DOWNSTREAM_API_BASE_URL=https://api.example.com
# local mock
DOWNSTREAM_API_BASE_URL=http://localhost:3001
If you tell me your language/framework and whether you have an OpenAPI spec, I can suggest the best tool and show a concrete setup.