Reference requests and examples in local mock servers
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.
In the following examples, <request-path> and <example-path> are placeholders for the path to a saved request and example in your local Git repo. The example’s status code, headers, and body are all sent as the response.
Rather than looking up paths manually, Postman provides a searchable dropdown directly in the mock code editor. When you click the argument, a dropdown list displays where you can search your workspace’s requests and examples by name or path.
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:
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.
Below is a complete example using the pm.mock API:
Path variable matching
The matching algorithm supports path variables, which are URL segments prefixed with : that match any value in that position.
Example: