Mock an API

A mock server is a public HTTP API that answers with the responses you define. There is no tunnel, no agent and no local port: Mercur serves it for you, so a frontend, a partner, a CI job or an AI agent can call an API that does not exist yet, or one that is too slow, flaky or expensive to call for real.

Mock servers are on Free, Solo and Team. A guest cannot create one. The numbers for each plan are below and on pricing.

Open Mock Servers

1. Create a server

In the console, open Mock Servers and choose New mock server. A name is optional. Mercur gives the server its own address:

Your mock server
https://mk-xxxxxxxx-xxxxxx.mercur.sh

The mk- part is yours and does not change. Turn off the Running switch at the top of the server to pause it without deleting anything; a paused server returns 503 with mock_server_disabled.

2. Add a route

A route is a method and a path. Open the Routes tab and choose Add route; it starts as GET / answering 200 with {}.

  • :name in a path captures a value: /users/:id matches /users/42.
  • A final * captures the rest of the path: /files/* matches /files/a/b.txt.
  • A route can name several methods, or ANY. A HEAD request falls back to the GET route, and with CORS on, OPTIONS is answered for you.
  • A single trailing slash is ignored, and paths are case-sensitive.

Routes are tried in order, and the first one that matches the method and the path and has a response whose conditions pass answers. If an earlier route would always win, the editor warns you that the later one is shadowed. Use the arrows to reorder.

Each route has an Enabled switch. A disabled route is skipped when matching, so you can turn a route off without deleting it.

3. Shape the responses

Each route has up to 20 responses. A response has a status (200 to 599), up to 20 headers, a body, and an optional delay of up to 10 seconds. The body is JSON, text or empty.

Responses are tried in order. A response with no conditions always matches, so keep it last as the default. Conditions pick between responses by what the request carried:

Read fromExample key
Path parameterid
Query stringpage
Header (name is case-insensitive)x-api-key
Body (JSON or form data, dotted path)user.role, items[0].id
OperatorMatches when the value
equals / does not equalis, or is not, exactly the text
contains / starts withcontains, or starts with, the text
matches regexmatches the pattern (up to 256 characters; a pattern that takes too long counts as not matching)
existsis present, or absent
greater than / less thancompares as numbers

A response can require all its conditions or any of them (at most 10 conditions per response). A condition on a value the request did not carry does not match, with two exceptions that do: does not equal, and exists when it is set to absent.

4. Fake data and templates

A body is a template: {{ ... }} is replaced when the request arrives. You can use:

ExpressionValue
request.method, request.paththe method and the path
request.params.ida path parameter
request.query.pagea query parameter (the first one if it repeats)
request.headers.x-api-keya request header (lower-case name)
request.body.user.namea field of a JSON or form body
now, uuidthe current time, a new UUID
indexthe position inside a list
faker.person.fullNamea generated value (see below)

A value that is not in the request becomes an empty string, or null when it is the whole value in a JSON body. Write a literal {{ as \{{; in the JSON text that is \\{{.

In a JSON body, a string that is exactly one expression becomes a real number or boolean, and an object with $repeat becomes a list:

{
  "id": "{{request.params.id}}",
  "name": "{{faker.person.fullName}}",
  "age": "{{faker.number.int(18, 90)}}",
  "orders": [{ "$repeat": { "min": 1, "max": 3 }, "item": { "id": "{{faker.string.uuid}}", "n": "{{index}}" } }]
}

age is sent as the number 42, not the text "42". In a text body, use {{#repeat 3}}line {{index}}{{/repeat}}. A render can expand to at most 1,000 list items, nested 2 levels deep; past that the relay answers mock_template_error.

Insert faker in the body editor lists 110 generated values (names, emails, addresses, prices, dates, ids and more), each with a sample, grouped by module. Pick a value and it is put at the cursor: inside a string it goes in as it is, outside one it is written as a string of its own, so the JSON stays valid. Values with options open a small form, and As a list of inserts a repeating list.

Suggestions appear as you type in the body editor. After {{ it offers request, faker, now, uuid and index. After request. it offers the parts of a request, and after request.params., request.query., request.headers. or request.body. it offers the names this route already uses: the :parameters of its path and the keys its conditions look at (a header list also has the common ones). After faker. it offers every generated value, found by its module or by its name. Move with the arrow keys, choose with Enter or Tab, close with Esc, and open them again with Ctrl+Space. A finished expression gets its closing }}. A name you type yourself is always fine; the list only helps.

Format (or Shift+Alt+F) lays a JSON body out with two-space indentation, one item per line. It changes only whitespace: strings, numbers, the order of the keys and the templates inside them stay exactly as you wrote them, and Ctrl+Z undoes it. A body that is not valid JSON is left as it is; the problem is shown and the cursor moves to it.

Generated values follow the server's Fake data locale (Settings): en, uk, de, fr, es, pl, it, pt_BR, ja, nl.

Each response has a seed. With random the data changes on every request. With a fixed number, or from a request value such as request.query.userId, the same input always gives the same data, so the user with that id is always the same person.

The editor checks a template as you type and shows each problem with its line and column; Save runs the same check again, and the server checks once more.

5. Try it

Under a route, Preview runs the saved server through the same engine the relay uses:

  • This response sends a sample request (the route's first method on a sample path, /users/1 for /users/:id) and shows that response, whatever its conditions say. Regenerate sends it again, so a random seed gives new data.
  • Whole server sends a request you write (method, path and query, headers, body) through every route, exactly as a visitor's would go, and shows which route and response were chosen and why.

The preview runs the saved version, so it is off while the route has unsaved changes. It shows the delay it would apply instead of waiting for it, does not count against your quota, and is rate limited. Copy as cURL (on the server and on each route) copies a command you can run:

Call a route
curl -i https://mk-xxxxxxxx-xxxxxx.mercur.sh/users/42?vip=1

6. Start from real traffic

You rarely have to write a response by hand.

  • Mock this. On a request in an endpoint's Requests tab, Mock this makes a route from it, with the response that was captured. It needs a request that was forwarded and whose response was saved (full traffic capture on the endpoint). The route uses the method and path the client sent, with numbers and UUIDs offered as :id, :id2. Headers a mock cannot carry are left out and listed, and a {{ in the captured text is escaped so it is served as written. A response that is HTML, binary, too large, outside 200-599, or for a method a route cannot name is refused with the reason.
  • Create route from this request. On a request in a mock server's own Requests tab that no route answered, this adds a route for it that answers 200 with {}.
  • Make dynamic. On a JSON body that is still a plain captured sample, it proposes generated values for emails, names, ids, dates and links, shows what would change, and applies the ones you tick.

A server with no routes offers two cards: From captured request, which makes a route from a recent tunnel request with a saved response the same way Mock this does, and Blank route.

7. See what happened

The Requests tab lists the calls to the server as they arrive. A row opens into the request, the response, and a Match trace: every route tried and why it did or did not match, which response was chosen, how each condition came out, and the seed. A request that no route matched is answered with the server's not found response.

Every response a route or the not-found response sends carries two headers that say the same thing:

HeaderValue
x-mercur-mock-routethe id of the route that answered, or none
x-mercur-mock-responsethe id of the response that was sent, or none

The errors the relay itself returns (see below) carry a different header, x-mercur-mock-error, instead of these two. The one exception is mock_template_error, which carries all three.

The log keeps a bounded number of the most recent requests per server, the same number as for endpoints (see Limits). Cookies a visitor's browser sends are removed before a route sees the request, and are not logged.

Settings

  • Allow cross-origin requests (CORS) (on by default) sends access-control-allow-origin: *, answers browser preflight requests, and never allows credentials. A header you set on a response wins over these defaults.
  • Fake data locale sets the language of generated data.
  • Not found response is what is sent when no route matches. By default it is a 404 with a JSON body that names the method and path.

Security rules

Mock servers live on a subdomain of the same domain as the console, so they are held to rules a tunnel is not:

  • No code runs. Templates are read by a small parser, not executed, and code of yours never runs on Mercur.
  • No cookies. Set-Cookie and other headers the relay owns (content-length, transfer-encoding, connection, host, content-encoding and a few more) cannot be set, and the editor refuses them as you type.
  • No pages. A response cannot have an HTML, XHTML, SVG or multipart/x-mixed-replace content type, in any case or inside a list of types. A mock serves data, not pages.
  • Locked down. Every response is sent with x-content-type-options: nosniff and content-security-policy: sandbox.
  • Reserved names. Header names that start with x-mercur- belong to Mercur and cannot be set.

Errors the relay returns

A request that no route matches gets your not-found response, which is an ordinary response. These are answered by the relay itself, as JSON {"error": "<code>"} with the code repeated in an x-mercur-mock-error header:

CodeStatusCause
mock_server_not_found503No mock server has this address.
mock_server_disabled503The server is paused.
mock_server_plan_disabled503Your plan no longer includes this server.
mock_quota_exceeded429The workspace used its mock requests for the month.
mock_request_too_large413The request body is over your plan's payload cap.
mock_too_many_concurrent503

Too many delayed responses are being held at once. They take slots from the same concurrent-session allowance as the tunnel sessions of your workspace, so a burst of delayed mock responses can also make tunnel endpoints answer "Too many concurrent sessions", and busy tunnels can use up the slots a delayed response needs.

mock_template_error500A template could not be rendered (for example a list that grew past its budget).
mock_internal_error500Something failed on Mercur's side.

Limits

Mock requests are counted on their own meter, separate from tunnel traffic, and the relay refuses them with mock_quota_exceeded once the month's allowance is used.

LimitFreeSoloTeam
Mock servers1310
Routes per server1050200
Mock requests per month5,00050,000250,000

The same on every plan: 20 responses per route, 10 conditions and 20 headers per response, a delay of up to 10 seconds, a body template of up to 256 KB, and a whole server of up to 1 MiB. See Limits for every plan limit side by side.

If you move to a plan that allows fewer servers, the oldest ones stay and the rest show Plan limit: they answer mock_server_plan_disabled, can be read and deleted, and cannot be edited or resumed until the plan allows them again. A server over its route limit keeps working and can be edited, but cannot get new routes.

What mock servers do not do

  • They are stateless: a POST does not change what a later GET returns.
  • They speak HTTP. WebSocket and TCP are served by tunnels, not by mock servers.
  • There is no code step. Conditions, templates and generated values are the whole toolbox.