Testing APIs

Mock server

Answer a collection's requests with example responses on your computer — build the frontend before the API exists, or keep testing while it's down.

The mock server pretends to be your API. It answers the requests of a collection (or folder) with each request's example response, on a port of your computer — so a frontend, a mobile app or another team's tests can work against it before the real API is ready, while it's down, or without touching shared test data.

Give requests an example

Each saved request can have one example: the status, headers and body the mock answers with.

  • From a real answer: send the request, then choose Save as example at the top of the response. Save the request to keep it.
  • By hand: the Example tab — set the status, headers (one per line, Name: value), the body, and a wait before answering if you want it to feel like a slow server.
  • From Postman: a request's saved example comes along when you import the collection (the first successful one).
  • From an OpenAPI file: imported requests already have one — the file's example for the success answer, or one made from its schema.

The body can use values: {{id}} is what the caller put in place of {{id}} in the path, a query parameter like {{page}} is filled from the address, and built-in values like {{$uuid}} or {{$randomInt(1,100)}} are new on every call.

{ "id": "{{id}}", "status": "paid", "ref": "{{$uuid}}" }

Start it

Open Mock server — from a folder's ⋯ menu (Mock server for this folder), the link on the Example tab, or the command palette. Pick the collection or folder and a port (4010 unless you change it), and press Start. It shows its address, for example http://127.0.0.1:4010, every request it answers, and the calls it gets.

Point your app at that address — or set {{baseUrl}} to it in an environment and send your saved requests to the mock.

It keeps running until you press Stop or close the app. Only this computer can reach it. Any web page on this computer may call it (it sends Access-Control-Allow-Origin: *), so a frontend in the browser works without a proxy.

How it picks the answer

  • The method and path of the call are matched against the saved requests. The path comes from each request's address: {{baseUrl}}/orders/{{id}} answers /orders/A-1001, /orders/7… — a {{name}} stands for any one part of the path. The query string doesn't matter.
  • When two match, the one with more fixed parts wins: /orders/latest before /orders/{{id}}.
  • A request without an example answers 501 and says so. A call that matches nothing answers 404 with the list of paths it knows.
  • Changes to an example are used at once — no restart.

From the command line

npx apitestcenter mock "Orders API" --port 4010 --data api-tests

atc mock runs it until Ctrl+C, printing each call. In a pipeline, start it in the background before your frontend's tests. "" mocks the requests outside any collection.

Something unclear or missing? Write to support@apitestcenter.com — the guide improves with every question.