Prompt

How do I mock a third-party API for local development?

Technology / Testing & QA1 observationLast seen Oct 6, 2026

Latest observation

Oct 6, 2026OpenAI APIWeb search: off

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:

  1. Create a small mock server or use Mockoon/WireMock.
  2. Put mock responses in JSON fixtures.
  3. Configure your app with API_BASE_URL or a feature flag.
  4. Add a few tests for error and edge cases.
  5. Keep the real API client unchanged so switching is easy.

When to choose what

  • Simple CRUD API → json-server or 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.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as Circuit. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.