Keep data between requests
Before you begin
- Plan
- Included in Free, Pro and Team. How plans and limits work
- Role
- Editor or higher to set up a mock, Viewer or higher to see the records and Editor or higher to change and clear records.
- You need
- A project; see Create and edit a mock
A project’s data store holds JSON records that its mocks share. A mock can save a record after it answers, can answer only while a record exists, and any mock’s response body can read the records back. With five mocks, POST /users followed by GET /users/u1 returns the user that was posted.
| Mock | Request | Data store | Response body |
|---|---|---|---|
Create user |
POST /users |
Save the record in users, key from the response body at $.id |
{"id": "{{jsonPath request.body '$.id'}}", "name": "{{jsonPath request.body '$.name'}}"} |
Read user |
GET /users/{id} |
Answer only while the record in users, key from the path parameter id, exists |
{{{lookup store.users request.path.id}}} |
Missing user |
GET /users/{id} |
None | 404, no body |
List users |
GET /users |
None | {{{toJson (storeList store.users)}}} |
Delete user |
DELETE /users/{id} |
Answer only while the record exists, then delete it | 204, no body |
Read user and Missing user match the same requests. A mock with a data store condition is always tried first, so Missing user answers only when the record does not exist.
Save a record
Section titled “Save a record”-
Create the
Create usermock with New mock on the project’s Mocks tab: methodPOST, path/users, status201, and the body above withContent-Type: application/json. See Create and edit a mock. -
In Data store, turn on Change the store after answering.
-
Leave Then at Save the record, and enter
usersin Collection. -
Leave Key from at Response body and JSONPath at
$.id, and Saving at The response body. -
Select Save mock.
The mock answers first, then saves its own response body as the record. To save what the caller sent instead, set Saving to The request body. Either way the value must be JSON.
Let the mock choose the id
Section titled “Let the mock choose the id”Create user saves the record under the id the caller sent. To have the mock make up the id, as a real API does, generate it in the response body instead:
{"id": "{{randomValue type='UUID'}}", "name": "{{jsonPath request.body '$.name'}}"}Keep Key from at Response body with JSONPath $.id, and Saving at The response body. The mock renders its response once, saves it under the id it holds, and sends the caller that same response, so POST /users with {"name": "Ada"} answers 201 with an id such as {"id": "1b4e28ba-2fa1-41d2-883f-0016d3cca427", "name": "Ada"}, and GET /users/1b4e28ba-2fa1-41d2-883f-0016d3cca427 then answers the record. Only Response body can key a generated id: every other source reads the request, which does not hold it.
Answer only while a record exists
Section titled “Answer only while a record exists”-
Create the
Read usermock with a Path with parameters of/users/{id}and the body above. -
In Data store, turn on Check for a record before answering.
-
Enter
usersin Collection, leave The record at exists, set Key from to Path parameter, and chooseidin Path parameter. -
Optionally, select Copy under Answer with the record it found and paste the template as the response body. It answers with the record, indented.
-
Select Save mock.
A key can name any parameter that fills a whole part of the path: /files/{name}.json has none to offer. The key is the value as sent, so u1 is the key for /users/u1. For a Regex path, set Key from to Path segment and pick the segment from the mock’s path parts under the fields. Segments count from 0: in /users/u1, segment 0 is users and segment 1 is u1. The editor says what the chosen segment reads, and warns when it is fixed, such as users, or past the end of the path: every request would then look up the same key, and a condition on it would never match the record you saved. Set The record to does not exist for a mock that answers only while the record is missing, such as a 409 for a duplicate create.
For Delete user, turn on both switches: the condition as for Read user, and Change the store after answering with Then set to Delete the record, the same collection and the same key. Empty the collection removes every record in it and takes no key.
Read records in a response
Section titled “Read records in a response”Any mock’s response body reads the store as store.<collection>, whether or not the mock has a data store of its own:
| Template | Renders |
|---|---|
{{{lookup store.users request.path.id}}} |
The record whose key is the path parameter id, as JSON |
{{#formatJson}}{{{lookup store.users request.path.id}}}{{/formatJson}} |
The same record, indented |
{{{toJson (storeList store.users)}}} |
Every record in users as a JSON list, oldest first |
{{size store.users}} |
How many records users holds |
Use three braces, so the JSON is not HTML-escaped. A record prints on one line. To indent it, wrap the lookup in the block form of formatJson, as in the second row; formatJson with the lookup as an argument answers an error, because a record is not text. A collection nothing has written reads as empty. See Template helpers for lookup, toJson, size and storeList.
In the response body editor, typing store. lists the project’s collections. A collection that no mock saves to and that holds no records is marked with a warning, because it reads as empty; the name usually has a typo.
See and edit the records
Section titled “See and edit the records”-
Open the project’s Data store tab. It shows the records and size the project holds against its plan, and each collection with the mocks that use it.
-
Select a collection to open its records as a table.
-
To remove records, select Clear on a collection, or Clear the store for all of them, and confirm.
On a collection’s page you can also search, sort and filter its records, add, change and delete them, import and export them, and save a snapshot to put them back later; see Edit data store records. Once a snapshot exists, Reset puts the store back to it in one step, which makes a test loop of run, inspect, reset; see Reset to a snapshot.
An AI assistant connected to MockHub does the same with the get_store, list_store_records, get_store_record, put_store_records and clear_store MCP tools, and can give a mock a data store when it creates or changes one.
Result
Section titled “Result”POST /users with {"id": "u1", "name": "Ada"} answers 201 and saves the record; GET /users/u1 then answers it, GET /users lists it, and after DELETE /users/u1 the GET answers 404. The record shows on the Data store tab under users.
When a write cannot happen, the caller gets an error instead of the mock’s response, and nothing is saved:
| Status | Error type | Why |
|---|---|---|
422 |
Data store key missing | The key is not where the mock reads it |
422 |
Data store value invalid | The value to save is not JSON |
507 |
Data store limit | The store is full, or the record is too large |
507 |
Data store unavailable | The workspace’s plan does not include the data store |
Limits
Section titled “Limits”- The store is test data, not a database. MockHub keeps it until you delete the records or the project; records do not expire. A failure on MockHub’s side can lose about the last second of writes, and MockHub’s backups do not include the store.
- Scenario state and response-sequence positions are kept beside it, but those are dropped after 7 days unused, each on its own clock: reading or writing the store does not keep a scenario’s state.
- Anyone who can call the project’s mocks can read what they return, records included. Keep real personal data out of it.
- How many records and bytes a project may hold is set by the plan; see the limits page. A record is at most 64 KB, a key at most 256 bytes, and a project has at most 20 collections. A collection name starts with a lowercase letter and holds letters, digits and underscores.
- The configuration document carries each mock’s data store definition, not the records; its
seedand the Data store tab fill them. - Try it checks the condition against a store of its own: empty, or a copy of the project’s records of at most 1 MB. What it writes never reaches the project.
- A write is skipped when a chaos profile injects an error instead of the mock’s response.
- After a downgrade to a plan without the data store, mocks keep their definitions, but writes fail with Data store unavailable, conditions never match and templates read an empty store.