Prompt

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

Technology / Testing & QA1 observationLast seen Oct 6, 2026

Latest observation

Oct 6, 2026OpenAI APIWeb search: off

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

  1. Record or define the API responses you expect.
  2. Start a local mock server on something like http://localhost:8081.
  3. 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.

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.