***
title: Reference requests and examples in local mock servers
updated: 2026-03-02T00:00:00.000Z
topictype: reference
slug: docs/tests-and-scripts/write-scripts/postman-sandbox-reference/pm-mock
max-toc-depth: 2
----------------
The `pm.mock` object provides structured, Postman-aware functions for matching incoming requests and sending responses with a local mock server. The `pm.mock` API can serve responses from your existing saved Postman examples rather than hard-coding everything.
The
`pm.mock`
object is supported only in the mock code editor in Local View in the Postman desktop app.
## pm.mock
Use the `pm.mock` methods to match incoming requests and send responses, including responses from your existing saved Postman examples.
### pm.mock.matchRequest()
Matches an incoming request against a Postman request by its method and path. Returns true if the incoming request matches the specified criteria.
Example:
```js
if (pm.mock.matchRequest('', req)) {
res.status(200).json([{ id: 1, name: 'Alice' }]);
return;
}
```
You can also pass a Postman request ID (the unique ID of a saved request in your collection) instead of specifying the method and path. This matches using the saved request's method and URL.
### pm.mock.sendExample()
Sends a saved Postman example as the HTTP response. This is the key integration point between your existing Postman collection data and your mock server.
```js
if (pm.mock.matchRequest('', req)) {
pm.mock.sendExample('', res);
return;
}
```
In this example, `` is the ID of a saved request in your Postman collection and `` is the ID of a [saved example](/docs/sending-requests/response-data/examples#save-a-response-as-an-example) under that request. The example's status code, headers, and body are all sent as the response.
Rather than looking up IDs manually, Postman provides a searchable dropdown directly in the mock code editor. When you click on the string argument of `matchRequest` or `sendExample`, a dropdown list appears where you can search your workspace's requests and examples by name or URL path.
Below is a complete example using the `pm.mock` API:
```js
// Match GET /users and serve the saved "List Users - 200 OK" example
if (pm.mock.matchRequest('', req)) {
pm.mock.sendExample('', res);
return;
}
// Match GET /users/:id with a path variable
if (pm.mock.matchRequest('', req)) {
if (req.params.id === '999') {
res.status(404).json({ error: 'User not found' });
} else {
pm.mock.sendExample('', res);
}
return;
}
// Match POST /users
if (pm.mock.matchRequest('', req)) {
pm.mock.sendExample('', res);
return;
}
res.status(404).json({ error: 'Route not matched' });
```
### Path variable matching
The matching algorithm supports path variables, which are URL segments prefixed with `:` that match any value in that position.
Example:
```js wordWrap
// Matches /products/42, /products/abc, /products/anything
if (pm.mock.matchRequest('', req)) {
console.log('Requested product ID:', req.params.id);
res.status(200).json({ id: req.params.id, name: 'Example Product' });
return;
}
// Nested path variables also work
if (pm.mock.matchRequest('', req)) {
res.status(200).json({
orgId: req.params.orgId,
userId: req.params.userId
});
return;
}
```
The matching algorithm matches on HTTP method and URL paths only. It does not match on query parameters or request headers.
## Mock your API with a response example
To customize your local mock server implementation, you need to create a collection with a request to your API and save the response as an example.
1. Click the
Development tab in the sidebar.
2. [Create a collection](/docs/collections/use-collections/create-collections).
3. [Create and send a request](/docs/sending-requests/create-requests/request-basics) to your API.
4. In the response area, click
**Save Response** to save the response as an example.
For the request and example, you can click
in the right sidebar and copy the IDs for later.