Prompt
How do I mock a third-party API for local development?
Latest observation
Mocking a third-party API for local development usually means giving your app a fake version of the external service so you can build and test without hitting the real API.
Common approaches
1) Run a local mock server
Create a small HTTP server that returns the same kinds of responses the real API would.
Good for:
- full end-to-end local testing
- predictable responses
- working offline
How:
- Use something like:
- json-server for simple REST-ish data
- WireMock for more realistic HTTP mocking
- Mockoon for a GUI-driven mock API
- Prism if you have an OpenAPI spec
- a custom app in Express/FastAPI/Flask/etc.
Example idea:
If your app calls GET https://api.vendor.com/users/123, your local mock server could respond with:
{
"id": 123,
"name": "Test User"
}
Then configure your app to call:
http://localhost:3001/users/123
2) Mock at the HTTP client layer
Instead of running a server, intercept requests in your application code/tests.
Good for:
- unit/integration tests
- fast feedback
- no extra server process
Examples:
- MSW (Mock Service Worker) for browser apps
- nock for Node.js
- responses or requests-mock for Python
- httpretty for Python
- sinon stubs in JS for wrapper functions
This is especially useful if you want the app to think it’s talking to the real API, but the requests are intercepted and fake responses are returned.
3) Use dependency injection or an abstraction layer
Wrap the third-party API in your own client interface, then swap implementations:
- real client in production
- mock client locally
Good for:
- clean architecture
- easier testing
- less brittle code
Example pattern:
// production
const apiClient = new RealApiClient();
// local/dev
const apiClient = new MockApiClient();
4) Record and replay real responses
Capture actual API responses once, then replay them locally.
Good for:
- matching real behavior closely
- reducing manual fixture maintenance
Tools:
- VCR.py for Python
- Polly.js for JS
- WireMock record/replay mode
Be careful with:
- secrets
- PII
- stale recorded data
Best practices
Make the mock configurable
Use environment variables so you can switch between real and mock easily:
API_BASE_URL=http://localhost:3001
Keep responses realistic
Include:
- success cases
- error cases
- timeouts/retries if relevant
- pagination
- auth failures
- rate limit responses
Mirror the real API contract
If possible, use:
- the provider’s OpenAPI spec
- examples from their docs
- captured responses from production/dev
Version your mock data
Treat fixtures like code:
- store them in git
- update them when the real API changes
- write tests to catch drift
Recommended setup for local development
A practical setup is:
- Create a small mock server or use Mockoon/WireMock.
- Put mock responses in JSON fixtures.
- Configure your app with
API_BASE_URLor a feature flag. - Add a few tests for error and edge cases.
- Keep the real API client unchanged so switching is easy.
When to choose what
- Simple CRUD API →
json-serveror a custom mock server - Complex API with auth/state → WireMock / Mockoon
- Frontend app → MSW
- Node backend tests → nock
- Python backend tests → responses / VCR.py
- Need high fidelity → record/replay or contract-based mocks
If you tell me your stack (JavaScript, Python, Java, Go, etc.) and whether this is for frontend, backend, or both, I can suggest the best tool and show a concrete example.