What a mock API is: a stateful fake API so the front end and tests need not wait for the back end

  1. What a mock API is and what it solves
  2. Static mocks and stateful mocks
  3. Hands-on with Localman
  4. 1. Create a project
  5. 2. Add a resource and generate data
  6. 3. The five CRUD endpoints
  7. 4. Do not test only pretty data
  8. 5. Rules: conditional errors
  9. 6. Chaos: slow and flaky
  10. When the real back end is ready

What a mock API is and what it solves

A mock API is an HTTP service that plays the real back end: it accepts requests on the paths, methods and JSON formats both sides agreed on, and returns sample responses.

The app calls it like the real API; once the back end is ready, only the base URL changes.

A mock solves three common problems:

Many UI bugs are really contract bugs: the client sends the wrong field name, the API returns an array while the client expects an object, a failed sign-in has no proper status code, empty data breaks the layout.

Mocking early surfaces them before integration.

Static mocks and stateful mocks

Hands-on with Localman

Localman is a stateful REST API mocking tool that runs on the web. The steps below follow its "Quick start" documentation.

1. Create a project

On the Projects page, create a project and, if you like, set an API Prefix such as /api/v1. Each project has a secret that sits right in the mock URL:

https://<secret>.localman.hoandat.com/api/v1/...
https://localman.hoandat.com/m/<secret>/api/v1/...

Use the second form when your network or tool cannot resolve the project's own subdomain.

The secret works like a password: anyone with the URL can read and write the mock data, so never put real personal data in it.

2. Add a resource and generate data

A resource is a set of records with a plural name such as products; the name becomes the first segment of the route.

The schema lists each record's fields, each one a basic type or a Faker method that generates names, emails or dates. Then pick how many records to generate (up to 100 at a time).

3. The five CRUD endpoints

Every resource comes with:

Method

GET

Path

/products

Result

200, array of records

Method

GET

Path

/products/12

Result

200, one record

Method

POST

Path

/products

Result

201, the new record

Method

PUT / PATCH

Path

/products/12

Result

200, the record after merging fields

Method

DELETE

Path

/products/12

Result

200, the deleted record

The body of POST, PUT and PATCH is a JSON object sent with Content-Type: application/json. A quick try with curl:

curl 'https://<secret>.localman.hoandat.com/api/v1/products?page=1&limit=10'
curl -X POST https://<secret>.localman.hoandat.com/api/v1/products \
  -H 'Content-Type: application/json' \
  -d '{"name":"Keyboard","price":450000,"inStock":false}'

List routes accept search, filtering by field (?inStock=false), sortBy with order=desc, page and limit (at most 100 records per page).

What these parameters mean and the design choices behind them are in Pagination, filtering and sorting in a REST API: offset, cursor and the conventions to agree on.

Nest reviews under products and you also get /products/3/reviews.

4. Do not test only pretty data

For a product list screen, cover: many products, exactly one, an empty list (delete every record or filter to nothing), a product without an image, a very long name, an out-of-stock product.

Check that the empty state is clear, broken images have a fallback, and "load more" stops at the right time.

5. Rules: conditional errors

Each endpoint can have rules: when a request matches a condition, it returns a different status and body.

A condition matches on a header, a query parameter, a JSONPath into the body, or the request order (the Nth request). Rules run top to bottom and the first match wins.

For example, with a sessions resource used as a sign-in endpoint, POST /sessions could have:

The front end can then exercise both the redirect-on-success branch and the keep-the-form-and-show-an-error branch.

How to shape error bodies so clients act on a code rather than the wording is discussed in Consistent errors, idempotency and REST API versioning: conventions that help an API last.

6. Chaos: slow and flaky

Chaos makes an endpoint slow (a random delay between a minimum and a maximum, up to 30 seconds) or flaky by ratio: an error with a status of your choice, a 429, or hanging until the timeout and then a 504.

Use it to test spinners, retry logic and network error messages.

When the real back end is ready

  1. Write down the contract the mock serves (resources, fields, error codes) so both sides can compare.

  2. Switch the base URL from the mock to the real back end.

  3. Rerun every scenario you tried against the mock, error branches included; any difference is a place where the contract does not match.

  1. Rotate the secret or delete the mock project if its URL was shared widely.

What a mock API is: a stateful fake API so the front end and tests need not wait for the back end