Skip to content

Build a REST resource that remembers

Before you begin

Plan
Included in Free, Pro and Team. How plans and limits work
Role
Editor or higher.
You need
  • Response templating on in the project's settings

One scaffold turns a path such as /users into a small REST API over a data store collection: a list that filters, sorts and pages, create, read, replace, update and delete, and a 404 for an id that does not exist. What it makes is a set of ordinary mocks, tagged resource:<name>, that you can read, edit and delete like any other.

For users at /users, the scaffold plans one mock per answer the API gives:

Mock Request Answers
List users (GET /users) GET /users 200 with the records the query asks for, through storeQuery
Create user (POST /users) POST /users 201 with the request body and a generated id, and saves it
Read user (GET /users/{id}) GET /users/{id} 200 with the record, while it exists
Replace user (PUT /users/{id}) PUT /users/{id} 200 with the request body and the path’s id, and saves it in place of the record
Update user (PATCH /users/{id}) PATCH /users/{id} 200 with the record merged with the request body, and saves it
Delete user (DELETE /users/{id}) DELETE /users/{id} 204, and deletes the record
Missing user (* /users/{id}) Any method on /users/{id} 404 with a problem body, No user with this id.

A mock answers one kind of request, so a resource is several mocks. The four item mocks each answer only while the record exists, which puts them ahead of Missing user in the match order: an id the collection holds reaches its mock, and any other reaches the 404. Leave operations out, under Operations or in the assistant’s operations, and their mocks go with them; Missing user stays while any item operation is left. The CLI always plans every operation.

By default the create mock generates a UUID under id, as most APIs do. Its answer has no Location header, because a header is rendered apart from the body and cannot see the id the body printed; read the id from the response body.

With client ids (--client-ids in the CLI, idSource: client for the assistant; the New REST resource page always generates ids), the caller sends the id. The create mock then answers only while no record has that id, and sends Location: /users/<id>; one more mock, user exists (POST /users), answers 409 when the id is taken. That makes eight mocks.

The list mock answers an envelope by default:

{
"data": [{ "id": "u1", "name": "Ada", "status": "active" }],
"meta": { "total": 1, "page": 1, "size": 20, "pages": 1 }
}

With Array with X-Total-Count (--shape array in the CLI) it answers the bare list and puts the total in X-Total-Count. Callers filter, sort and page with query parameters:

Query Does
?status=active Keeps the records whose status is active
?status=active&status=new Keeps either
?age_gte=18 Compares: _gt, _gte, _lt, _lte, _ne
?name_like=ada Keeps names that contain ada, ignoring case
?sort=name,-createdAt Sorts by name, then by createdAt descending
?page=2&size=50 The second page of 50

Only the fields the mock lists filter and sort; any other parameter is ignored, so a cache-buster never empties the list. The fields are the ones you choose under Filters and Sort fields (--filters and --sort in the CLI), or else the top-level fields of the first example record that hold neither an object nor a list. The rules are in the storeQuery reference.

PUT saves the request body as the whole record, with the id the record already has, so a numeric id stays a number. PATCH merges the request body into the stored record: nested objects merge field by field, an array replaces the stored one, and a null is saved as null rather than removing the field.

A path with a parameter, such as /users/{userId}/orders, makes a nested resource when the assistant is given parent: { parameter: userId, field: userId }; the New REST resource page and the CLI take no parent. The list answers only the orders whose userId is the path’s, and a create saves the path’s userId into the record.

  1. On the project’s Mocks tab, select the arrow beside New mock (More ways to add mocks), then New REST resource. A project with no mocks shows New REST resource as a button of its own.

  2. Enter the Collection name, such as users, and the Path, such as /users.

  3. Under Source, keep Example JSON and paste one record or a list of up to 100:

    [
    { "id": "u1", "name": "Ada", "status": "active" },
    { "id": "u2", "name": "Grace", "status": "invited" }
    ]
  4. Under Operations, List shape, Filters and Sort fields, deselect what the resource should not have.

  5. Select Preview. The page lists each planned mock with its findings; select a mock to read its document.

  6. Select Create 7 mocks.

  7. Select Activate all, or Show the resource:users mocks to review them first. View records opens the collection’s page with Try it open, and Add records its import as well when nothing was seeded.

Every mock is created inactive, so nothing answers differently until you activate them. Changing the form after a preview drops the preview; select Preview again.

The other sources:

  • OpenAPI contract takes the Collection path and the Item path of the project’s OpenAPI contract, on a plan with contracts. The scaffold then plans only the operations the contract has, links each mock to its operation, takes the list’s filters from the list operation’s query parameters that name a field of the record, and seeds the contract’s own examples.
  • Existing collection takes the fields from the first record a collection already holds, and seeds nothing.

To start from data instead, create the collection first: on the Data store tab, select New collection, paste the records, and select Create collection. The collection’s page then offers Add REST endpoints, which opens New REST resource with Existing collection, the name and the path /<collection> filled in. Either way, the collection’s page is where the two meet: it lists the resource’s mocks under Endpoints, and its records as a table you can edit while clients call them. See Edit data store records.

  1. Put the example records in a file, such as users.json.

  2. Preview and create the mocks:

    Terminal window
    mockhub resource add users --path /users --from-example users.json --filters status --env staging

    The CLI lists each planned mock and any findings, then asks Create these 7 inactive mocks?.

  3. Answer yes. The CLI prints Created 7 mocks tagged resource:users, seeded 2 records.

  4. Run mockhub pull if the project’s configuration lives in mockhub.yaml: the new mocks are in the project, and the file is behind it.

Add --activate to step 2 to activate the mocks as well, and --yes to skip the question in a pipeline. --from-openapi /users /users/{id} and --from-collection choose the other sources; see CLI commands.

An AI assistant does the same with the scaffold_resource MCP tool: ask it to “scaffold a users resource at /users from these records”. It previews first and creates the mocks only once you agree.

Activate all on the result, or --activate in the CLI, activates every created mock. To activate them later:

  1. On the project’s Mocks tab, select Filters and enter resource:users in Any tag.

  2. Tick the mocks, or select every matching mock.

  3. Select Activate.

If activating them would take the project past its plan’s limit, none is activated. Seven mocks are a real share of a small plan; see the limits page.

Example records seed the collection only while it is empty: at most 100, each with the id field, within the plan’s data store limits. When the scaffold cannot seed, it says why with a seed-skipped finding and creates the mocks anyway. To fill the collection later, POST records to the resource, import them on the collection’s page (see Edit data store records), push a seed from mockhub.yaml with mockhub store seed (see CLI commands), or apply a fixture.

Each mock is an ordinary mock: open it from the Mocks tab and change its response, status or matching as usual. Its history, request logs and export work as for any mock. The scaffold never regenerates or overwrites what you changed; to start over, remove the resource and scaffold it again.

  1. On the Mocks tab, filter by the tag resource:users, select every matching mock, and select Delete.

  2. On the Data store tab, select Clear on the users collection, and confirm.

The collection’s page shows a Try it block under Endpoints: one curl command per endpoint, list, create, read, update and delete, with a copy button each. Item paths use the first record’s key, or KEY while the collection is empty, and create and update send that record without its id. For a private project, each command reads the access key from $MOCKHUB_KEY. An inactive mock shows Activate in place of its command.

After activating, POST /users with {"name": "Linus"} answers 201 with an id; GET /users?status=active lists Ada; PATCH /users/u1 with {"status": "away"} answers Ada with the new status; DELETE /users/u1 answers 204, and GET /users/u1 then answers 404.

  • A resource is a set of tagged mocks, not an object. The scaffold creates them once; nothing regenerates, renames or removes a resource except editing or deleting those mocks, and a project under config as code must pull them.
  • A create or update is not checked against a schema unless the project’s contract checks requests.
  • A request body that is not JSON is answered with Data store value invalid or a render failure, not a 400 of the resource’s own.
  • PATCH is a deep merge that keeps a null rather than removing the field.
  • A created record with a generated id gets no Location header.
  • A nested resource’s item mocks check the record’s id, not its parent. Deleting a parent deletes no children.
  • The list scans the whole collection on every request. It joins nothing, searches no text and offers no cursor, and filters on listed fields only.
  • There are no transactions. Two creates racing on one client-supplied id can both answer 201, and the second overwrites the first.
  • Records stay until you delete them or the project, and MockHub’s backups do not include them; see Keep data between requests.
  • The scaffold refuses a project whose response templating is off, rather than turning it on. Turn it on in the project’s settings first.
  • An OpenAPI import and a scaffold of the same contract each create a mock for the same operations. The scaffold warns, but does not adopt the imported mock.
  • The New REST resource page offers no client-supplied ids, nested resources or offset paging; the CLI offers client ids, and an AI assistant all three.

Report a problem with this page