Skip to main content

How to Mock API Calls in the Browser (2026 Guide)

ยท 18 min read
Daniel Caldas
Co-founder

To mock an API call is to answer a request your app makes with a response you wrote, instead of the one the server would send. The app cannot tell the difference: it receives the JSON, the status code and the headers you chose, and renders whatever they lead to.

You need it more often than you would think. The endpoint you are building against does not exist yet. A bug only shows up with a payload you cannot reproduce. The staging server is down on demo day. You want to see the screen for a 500, an empty list or a response that takes eight seconds. In all of these, waiting for the backend is the slow option.

This guide covers what mocking is, the three ways to do it and their trade-offs, then shows step by step how to mock a response right in the browser with tweak: no code, no proxy, no change to your app.

What mocking an API call meansโ€‹

A web app talks to its backend through HTTP requests, almost always made with fetch or XMLHttpRequest (XHR). A mock sits between the two: when a request matches a rule you defined, the mock answers it, and the real server is never asked.

Developers mock API calls to:

  • Build the frontend before the backend is ready. Agree on the response shape, mock it, and build the screen today.
  • Reproduce a bug. QA sends you the JSON that broke the page. Serve exactly that payload and the bug is on your screen.
  • Test the states nobody sees. Errors, empty lists, very long names, a slow endpoint: states the dev database never produces.
  • Demo without a server. A demo built on mocks works when staging does not.

We wrote a longer piece on the reasons in why developers mock HTTP requests, and a guide to frontend development without a backend if that is the situation you are in.

Three ways to mock an API callโ€‹

There is no single right tool. Each approach puts the mock in a different place, and that decides what it is good at.

ApproachWhere the mock livesBest atTrade-off
In code (Mock Service Worker, stubs)Your repositoryAutomated tests, a shared baseline for the teamSomeone writes and maintains every handler; usually off on staging
A proxy (Proxyman, Charles)A desktop app your traffic goes throughMobile apps, desktop apps, any HTTP clientProxy and certificate setup; reroutes traffic for the whole machine
A browser extension (tweak)Your browser, as rulesFast exploration on any page: localhost, staging, productionBrowser only; does not run in a test runner

In code. A library like Mock Service Worker lets you write request handlers in JavaScript and commit them next to your app. They run in the browser and in your tests, so they are the right home for the cases you want checked on every commit. The cost is that every case is code, and nobody writes a handler to try something once. Our comparison of Mock Service Worker and a browser extension goes through where each one fits, and how to use both.

A proxy. Desktop proxies see traffic from any app, not only the browser, which makes them the best fit for mobile and desktop apps. In exchange you configure the proxy and trust its root certificate. See tweak vs Proxyman for a fair side by side.

A browser extension. An extension intercepts requests inside the page, so there is nothing to install in your app and nothing to route. A rule works on whatever page you open: localhost, a preview deploy, production. It cannot help a test runner or a native app.

Most teams end up with two of these: handlers in code for the automated tests, and an extension for everything they want to see right now. If you are weighing specific tools, our alternatives page compares tweak with the other options.

How to mock an API call with tweak, step by stepโ€‹

tweak is a browser extension that mocks, modifies, redirects and delays HTTP requests, without code. In a nutshell it acts as a customizable API mock server within the browser. Here is the whole flow, from install to a mocked response on your page.

1. Install tweakโ€‹

tweak is available for Chrome, Edge and Firefox. The free plan needs no account. Pin it to the toolbar so the popup is one click away, then open the page whose API you want to mock.

2. Create a Mock ruleโ€‹

Open the tweak popup and click Create a new rule (the + in the toolbar). Every rule starts with the same two things:

  1. The URL. Type part of the request URL, for example /api/users. Matching is a partial string match by default, so /v1/user catches both //api.com/v1/user/123 and //api.com/v1/user/456. Turn on the regular expression toggle (.*) when you need a pattern instead. You can also paste a whole cURL command into this field, and tweak fills in the rule from it.
  2. The HTTP method. Pick the method the request uses in HTTP Method, or * to match every method.

Then check the Rule dropdown on the right. Mock is the default: it answers instead of the server.

The rule type dropdown in tweak, with Mock selected: answers instead of the server

3. Write the response: body, status and headersโ€‹

Now describe what the server should have said.

  • Response payload. Write the body in the Response payload tab, usually JSON. tweak warns you when the payload is not valid JSON, and serves it anyway, because a broken body can be exactly the case you want to test. See response payload.
  • Status code. On a Mock rule the status is required. Leave 200 for a success, or type 404, 422, 500. The error states guide covers which status codes to test and how to override the status of a real response.
  • Delay by (ms). 0 answers instantly. Any other value holds the response for that many milliseconds.

A tweak Mock rule for GET /api/users with status 200 and a JSON user object in the Response payload tab

  • Response headers. Open Response headers to set the headers your mocked response carries, one per row in the Key and Value columns: a Content-Type, a Cache-Control, a Retry-After for a rate limit. Header names that the browser does not let fetch or XHR set are flagged in red. See response and request headers, and how to override response headers for the use cases.

The Response headers tab of a tweak Mock rule with Content-Type, Cache-Control and X-Powered-By rows

4. Run itโ€‹

Rules only apply while tweak is running on the tab. Click the play button in the toolbar when you are ready to start intercepting requests. From that moment every active rule is enforced on that browser tab. Each rule also has its own switch, so you can keep a rule and turn it off without deleting it.

Then make the page send the request again: reload it, or repeat the action that calls the API.

5. Check the resultโ€‹

The page now renders your response. To confirm the rule is the reason, look at the counter next to the rule's switch: it shows how many requests the rule intercepted, and its tooltip shows when the last one happened.

The tweak counter tooltip: 3 requests intercepted, with the time of the last one

If the counter stays at 0, the rule did not match. Check the URL and the HTTP method against the real request first: that is almost always the cause. For more detail, switch on the rule's logpoint and every matched request is logged to the page's console.

Start from a real response with request captureโ€‹

Writing a realistic payload by hand is slow. It is faster to start from what the server really sends and change what you need.

Click the capture icon in the top bar and tweak starts recording the HTTP requests of the current tab (the icon turns red). Use the page to trigger the requests you care about, then add a rule and search for the request directly in its URL field. Pick it, and every field of the rule is prefilled: URL, method, status, response payload and response headers.

The tweak top bar with request capture on: the capture icon is red

Smart filters such as status:404 or res:(Ada Lovelace) (a value you saw on the page) narrow the search. Captured requests never leave your browser. Read more in request capture.

Mock or Modify: which one do you need?โ€‹

A Mock rule replaces the server entirely: the request never leaves the browser, and your rule is the whole answer. A Modify rule lets the request reach the server and then changes what comes back (or what goes out). Modify rules are available on Tweak Basic and Tweak Max.

How a modify rule's interface differs from a mock rule's in tweak

On a Modify rule, an empty Response payload forwards the original response, and an empty Status code shows an Original chip and keeps the server's status. So you can change one thing and leave the rest as the server sent it.

Use Mock when:

  • the endpoint does not exist yet, is broken or is slow;
  • you want a fixed answer you fully control.

Use Modify when:

  • you want the real response with one field, the status code or a header changed;
  • you want to rewrite the request payload before it is sent.

The full comparison, with diagrams, is in mock and modify rules.

Simulate slow responses and errorsโ€‹

Mocks are at their most useful for the states your backend will not produce on demand.

Slow responses. Set Delay by (ms) to 3000 or 8000 and watch the loading state for as long as you want: does the skeleton jump when the data lands, can the user navigate away, does your timeout fire? On a streaming response (Server-Sent Events or Transfer-Encoding: chunked) the delay is applied between each chunk. See how to simulate a delayed HTTP request.

Server errors. Set the status to 500, 503 or 429 and give the rule the error body your API really returns. Add a Retry-After row in Response headers for a rate limit. How to simulate an HTTP request error walks through it with a demo app.

Network errors. A network error is not a status code: the request gets no answer at all, and fetch rejects. Create a block rule for the endpoint and the browser cancels the request, so the page sees exactly what it would see on a dropped connection. Our post on how to block an HTTP request covers the details.

Broken payloads. Serve malformed JSON, or valid JSON with a field missing or null, and look for white screens and undefined on the page.

For a full checklist of failures worth testing, and what a good UI does with each, read how to test error states in your frontend.

Match the exact request you meanโ€‹

A URL and a method are often not enough to tell requests apart. A search endpoint answers every filter on the same URL, and every GraphQL operation goes to /graphql. The Request payload tab is how a rule picks requests by their body.

The bar above the editor offers three modes:

ModeCatches
Any (default)every request on the rule's URL and method, the body is not checked
Equalsonly a body strictly equal to the payload
Containsany body that includes the fields you list, whatever else it has

Any and Equals are free. Contains is available on Tweak Basic and Tweak Max. A form post (application/x-www-form-urlencoded or multipart/form-data) is edited as a key/value table and matched field by field, in any order. See matching requests by their payload.

Mocking GraphQL, one operation at a timeโ€‹

GraphQL is where Contains earns its place. Set the URL to /graphql, pick Contains and write { "operationName": "getCity" }. The rule now mocks every getCity query, whatever its variables, and a getCountry query on the same URL goes through untouched. One rule per operation is how you mock a GraphQL API one query at a time.

A tweak Mock rule catching every getCity GraphQL query with a Contains request payload filter

On Tweak Max, a response hook can branch on the operation name and the variables in JavaScript instead. Both techniques are in the GraphQL docs and in our guide to mocking GraphQL requests.

Headersโ€‹

On a Mock rule, the Response headers tab sets what the mocked response carries. If you need a header on every request (an auth token, a feature flag), a headers only rule is the better fit: it is applied by the browser itself, so it reaches requests your JavaScript never makes. CORS problems are a common reason to reach for one: see how to fix CORS errors in the browser.

Change one request by hand: Pause & Editโ€‹

Sometimes you want to stop the next request, change it, and see what happens. That is Pause & Edit, available on Tweak Max.

Switch it on in a Mock or Modify rule's debugging group and pick where to pause: the request, the response, or both. When the page sends a matching request, tweak holds it and opens an editor on the page itself. Edit the URL, the body, the headers or the response, then press Continue request or Deliver response. Cancel request ends it as an abort.

A paused request in tweak's Pause & Edit editor, framed over the page, with its body edited before it is sent

Edits apply to that one request only, and the rule keeps doing what it was saved to do. The walkthrough is in how to edit an HTTP request before it is sent.

What about Chrome DevTools Local overrides?โ€‹

Chrome has a built-in way to fake a response: Local overrides. In the Network panel, right-click a request (fetch and XHR included) and pick Override content. DevTools asks for a folder on your disk, saves the response there, and serves your edited copy on the next reload. Override headers does the same for response headers.

It is free, already installed, and good for one quick experiment on a single response or file. Where it stops:

  • it replaces content and response headers, but there is no field for the status code, a delay, or a match on the request body, so one GraphQL operation cannot be told from another;
  • the overrides are served by DevTools, as files in a local folder, and live in your DevTools setup rather than in something you can export and share;
  • every override takes a few steps, per request.

If you need any of those, a mocking tool is the next step. Our Chrome DevTools Network tab guide covers overrides, request blocking and throttling in detail, and what each is good for.

Limits worth knowingโ€‹

Mock and Modify rules work on the requests your page makes with fetch and XHR, which covers REST and GraphQL API calls. Two things they do not do, both listed in known limitations:

  • Service worker requests are not intercepted.
  • The body of documents (HTML, CSS, JavaScript files) cannot be rewritten. A redirect rule can point one of those requests at another URL instead, a local build for example.

FAQโ€‹

Can I mock an API without changing my code?

Yes. A browser extension such as tweak intercepts the fetch and XHR requests your page makes and answers them with the response you wrote in a rule. Your app and the backend stay as they are; turn the rule off and the real server is back.

Does mocking work with fetch and XHR?

Yes. Mock and Modify rules wrap fetch and XMLHttpRequest inside the page, so they see every API call your JavaScript makes, REST or GraphQL. They do not reach service worker requests, and cannot rewrite the body of HTML, CSS or JavaScript files.

Is tweak free?

Yes. The free plan covers up to 12 mock rules, plus header, redirect and block rules, with no account needed. Tweak Basic ($2 a month or $18 a year) adds unlimited rules, Modify rules and Contains payload matching. Tweak Max ($4 a month or $36 a year) adds Pause & Edit, variables, data generators and JavaScript hooks. Both paid plans have a 7-day free trial.

Can I mock GraphQL requests?

Yes. Every GraphQL operation goes to the same URL, so a rule tells them apart by the request body: on the Request payload tab, pick Contains and write the operationName, one rule per operation. Contains matching is on Tweak Basic and Tweak Max.

Does a mock rule affect other tabs or other sites?

No. Mock rules run inside the page, on the tab where you started tweak with the play button. Other tabs keep talking to the real servers until you start tweak there too, or add their site to your auto-enabled sites so tweak starts on it by itself.

Can I test a slow API or a server error with a mock?

Yes. Set the Delay by (ms) field of a Mock rule to hold the response for as long as you want, and set its Status code to 404 or 500 to serve an error. For a network error, where the request never gets an answer, use a block rule.


Install tweak and mock your first API call in a minute. Mock rules are free, and so are header, redirect and block rules.

If you liked this article, consider sharing (tweeting) it to your followers.



Did you like this article?

Need something else? Request a feature