For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Postman
PricingEnterprise
Contact SalesSign InSign Up for Free
HomeDocs
HomeDocs
      • Overview
        • Overview
        • Write pre-request scripts
        • Write tests
        • Script examples
        • Dynamic variables
          • Overview
          • pm variables methods
          • pm.vault
          • pm.cookies
          • pm.request
          • pm.response
          • pm.sendrequest
          • pm.visualizer
          • pm.test and pm.expect
          • pm.require
          • pm.execution
          • pm.message
          • pm.info
          • pm.mock
          • pm.datasets
          • pm.state
        • Troubleshoot test errors
Postman API Platform

Product

  • Postman Overview
  • Enterprise
  • Spec Hub
  • Flows
  • Agent Mode
  • API Catalog
  • Fern
  • Postman CLI
  • Integrations
  • Workspaces
  • Plans and pricing

API Network

  • App Security
  • Artificial Intelligence
  • Communication
  • Data Analytics
  • Database
  • Developer Productivity
  • DevOps
  • Ecommerce
  • eSignature
  • Financial Services
  • Payments
  • Travel

Resources

  • Postman Docs
  • Academy
  • Community
  • Templates
  • Intergalactic
  • Videos
  • MCP Servers

Legal and Security

  • Legal Terms Hub
  • Terms of Service
  • Postman Product Terms
  • Security
  • Website Terms of Use

Company

  • About
  • Careers and culture
  • Contact us
  • Partner program
  • Customer stories
  • Student programs
  • Press and media
Twitter iconLinkedIn iconGithub iconYouTube iconInstagram iconDiscord icon
Download Postman
Privacy Policy

© 2026 Postman, Inc.

On this page
  • pm.mock
Tests and scriptsWrite scriptsPostman sandbox reference

Reference requests and examples in local mock servers

||View as Markdown|
Was this page helpful?
Previous

Reference request metadata in scripts

Next

Manage and use datasets in scripts

Built with

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:

1if (pm.mock.matchRequest('<request-path>', req)) {
2 res.status(200).json([{ id: 1, name: 'Alice' }]);
3 return;
4}

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.

1if (pm.mock.matchRequest('<request-path>', req)) {
2 pm.mock.sendExample('<example-path>', res);
3 return;
4}

Below is a complete example using the pm.mock API:

1// Match GET /users and serve the saved "List Users - 200 OK" example
2if (pm.mock.matchRequest('<get-users-request-path>', req)) {
3 pm.mock.sendExample('<list-users-200-example-path>', res);
4 return;
5}
6
7// Match GET /users/:id with a path variable
8if (pm.mock.matchRequest('<request-path>', req)) {
9 if (req.params.id === '999') {
10 res.status(404).json({ error: 'User not found' });
11 } else {
12 pm.mock.sendExample('<get-user-200-example-path>', res);
13 }
14 return;
15}
16
17// Match POST /users
18if (pm.mock.matchRequest('<request-path>', req)) {
19 pm.mock.sendExample('<create-user-201-example-path>', res);
20 return;
21}
22
23res.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:

1// Matches /products/42, /products/abc, /products/anything
2if (pm.mock.matchRequest('<request-path>', req)) {
3 console.log('Requested product ID:', req.params.id);
4 res.status(200).json({ id: req.params.id, name: 'Example Product' });
5 return;
6}
7
8// Nested path variables also work
9if (pm.mock.matchRequest('<request-path>', req)) {
10 res.status(200).json({
11 orgId: req.params.orgId,
12 userId: req.params.userId
13 });
14 return;
15}
The matching algorithm matches on HTTP method and URL paths only. It does not match on query parameters or request headers.