Master HMPL Development Offline

How Mock APIs Work

Your front end is ready. The back end is not. You could sit and wait for the API team to finish, or you could fake the endpoints and keep building. That second path is what a mock API gives you, and the way it pulls off the trick is simpler than it looks.

 
HMPL Render Studio

Last updated: August 2026

🔴 What a mock API actually is

A mock API is a stand-in. It answers requests the same way a real server would, with a status code, headers and a body, except there is no server and no database behind it. You define the shape of the reply, and the mock hands it back. That is the whole idea.

Teams reach for one in a few common spots. A front-end developer needs to build a screen against an endpoint that does not exist yet. A tester wants to force a 500 error on demand to see how the app copes. Someone giving a demo needs predictable data that will not change halfway through. In each case, understanding how mock APIs work saves you from spinning up a real backend just to get a bit of fake JSON. If you only need throwaway records to fill a table, a dummy data generator is the quicker route; a mock API matters once you need the request-and-response round trip itself.

🟡 Intercept, match, respond

Every mock does three things in order. It intercepts a request before it reaches the network, it matches that request against a list of routes you defined, and it returns the response attached to the route that fits. Miss on the match, and a good mock lets the request carry on to the real network instead of failing.

In the browser, the cleanest way to intercept is to wrap the built-in fetch function. Your code calls fetch('/api/users/42') as normal, but a replacement version looks at the URL and method first. The rules of that function are documented in the MDN Fetch API reference. Matching is pattern work: a route written as /api/users/:id becomes a small regular expression, and the :id segment is captured so the value 42 can be dropped into the reply. That is why a mock can answer a thousand different user IDs from a single rule.

🟢 Beyond a static reply: state, sequences and slow networks

Three mock response modes: static, sequential and stateful

The plainest mock returns the exact same body every time. Real apps are rarely that tidy, so a capable mock offers three richer modes. A sequential route walks through a list of replies, so the first call returns one thing and the second returns another, which is perfect for testing a retry. A stateful route keeps a small store in memory: post a new record and it is remembered, then a later get returns the growing collection, so you can mock a create-read cycle without a database. And generated tokens let a reply carry a fresh value each time, like a new ID or the current timestamp, rather than a frozen number.

  • 🔵 Static: the same response on every call – fine for a read-only screen.
  • 🟠 Sequential: a different response per call, cycling through a list.
  • 🟣 Stateful: an in-memory store that changes as you post to it.

The other trick worth knowing is latency simulation. Networks are slow and flaky, and code that looks fine on instant local data can fall apart on a real phone connection. By adding a deliberate delay, or randomly failing a slice of requests, a mock lets you see the loading spinners and error states you would otherwise only meet in production. It is a cheap way to build something that survives a bad signal.

🟡 Specs, GraphQL and staying on your machine

Mocks do not have to be typed out by hand. An OpenAPI or Swagger file already describes every path, method and example response for an API, so a mock can read that document and generate the routes for you. GraphQL works a little differently: instead of many URLs, there is one endpoint, and the request body names the operation it wants. A GraphQL mock reads that operation name and returns the matching data, a model explained well on the official GraphQL site.

One quiet advantage of the browser approach is privacy. A mock that runs entirely on your machine never sends your draft endpoints, tokens or sample payloads to anyone, which is the same reasoning behind keeping other developer utilities local, covered in this piece on client-side data processing. It is worth being honest about the ceiling, though. A mock fakes responses; it does not run real business logic, enforce real authentication, or keep data after you close the tab. Treat it as scaffolding while you build, and swap in the real thing once it exists. You can try all of this hands-on in the Mock API Studio.

What is a mock API in simple terms?

It is a fake endpoint that replies like a real server, with a status, headers and body, but has no real backend behind it. You define the response and the mock returns it.

Why would I use one instead of the real API?

To keep building the front end before the backend exists, to force specific errors during testing, and to demo with predictable data. It removes the wait on another team.

How does a mock intercept a request?

In the browser it wraps the native fetch function. Your normal call is checked against defined routes first, and if none match, the request is allowed through to the real network.

How does route matching handle IDs?

A path like /api/users/:id becomes a pattern where :id captures whatever value is passed. That captured value can then be placed into the response, so one rule serves many IDs.

What is a stateful mock response?

A route that keeps a small store in memory. Posting a record adds it, and a later get returns the collection, letting you mock a create-and-read flow without any database.

Why simulate slow or failing requests?

Instant local data hides loading and error states. Adding delay or random failures surfaces the spinners and fallbacks your users will actually hit on a poor connection.

Can a mock come from an OpenAPI spec?

Yes. An OpenAPI or Swagger file already lists paths, methods and example responses, so a mock can read it and generate matching routes automatically instead of manual setup.

How is mocking GraphQL different?

GraphQL uses a single endpoint, and the request names the operation it wants. A GraphQL mock reads that operation name and returns the matching data rather than routing by URL.

What can a mock API not do?

It fakes responses only. It does not run real business logic, enforce real authentication, or persist data after the tab closes. Use it as scaffolding, then move to the real backend.

Choose a language

Top Tools Ranking

Network Total Views
15,298
Tracking Since
Jul 9, 2026

Click any tool to open in a new window