Prompt
How do I mock a third-party API for local testing?
Latest observation
There are a few common ways to mock a third-party API for local testing, depending on how realistic you need it to be.
1) Use a mock server
Run a local service that returns pre-defined responses for specific endpoints.
Good for: quick local development, predictable responses
Tools: WireMock, Mockoon, Prism, json-server, MSW (for browser/front-end)
Example flow
- Record or define the API responses you expect.
- Start a local mock server on something like
http://localhost:8081. - Point your app’s API base URL to the mock server instead of the real API.
Example with WireMock
You create stubs like:
{
"request": {
"method": "GET",
"url": "/users/123"
},
"response": {
"status": 200,
"jsonBody": {
"id": 123,
"name": "Alice"
},
"headers": {
"Content-Type": "application/json"
}
}
}
Then your app calls:
GET http://localhost:8081/users/123
2) Mock at the code level with unit tests
If you only need to test your own code’s logic, mock the HTTP client in your tests.
Good for: fast tests, isolated behavior
Tools: Jest, pytest + responses, unittest.mock, Mockito, nock, requests-mock
Example in Python
If your code uses requests.get, you can mock it:
from unittest.mock import patch
import requests
def fetch_user():
return requests.get("https://api.example.com/users/123").json()
@patch("requests.get")
def test_fetch_user(mock_get):
mock_get.return_value.json.return_value = {"id": 123, "name": "Alice"}
assert fetch_user()["name"] == "Alice"
3) Use contract-based mocking
Define the expected request/response contract, then generate or validate mocks from it.
Good for: teams, integration consistency
Tools: OpenAPI + Prism, Pact, WireMock
If the third-party API has an OpenAPI spec, you can often auto-generate a mock server from it.
4) Record and replay
Capture real API responses once, then replay them locally.
Good for: mimicking real behavior without live calls
Tools: VCR.py, Betamax, Polly.js
This is useful when the API behavior is complex, but be careful with sensitive data and changing responses.
5) Build a small fake service
Write a tiny local app that imitates the endpoints you use.
Good for: custom logic, stateful behavior, complex workflows
Example: a Flask/Express app that returns dynamic results based on request parameters.
Recommended approach
- Frontend app: MSW or Mockoon/WireMock
- Backend service tests: mock the HTTP client in unit tests
- Integration/local development: WireMock, Mockoon, or a small fake service
- If you have an OpenAPI spec: Prism is especially convenient
Practical tips
- Make the base URL configurable via environment variables.
- Match status codes, headers, and error cases too—not just success responses.
- Mock timeouts, 429 rate limits, and 500 errors.
- Keep mock data close to real-world shapes.
- Separate “happy path” and “failure path” fixtures.
If you tell me your language/framework (e.g. Python, Node, Java, frontend React), I can show the best mocking approach with an example.