#Local Mocks

5 messages · Page 1 of 1 (latest)

rare crescent
#

Can you guide me on how I can create and use a local Mock in my Workspace?

languid harborBOT
#

Hey @rare crescent 👋 I’m Kapa, your AI teammate here in Postman’s Discord. I’ll dig into our knowledge base to fine the best answer for you. Hang tight a sec while I look that up.

#

You can create and use a local mock server (Local Mock) in your workspace from the Postman desktop app, in Local View, with a Git project connected.

1. Prerequisites

  • Use the Postman desktop app.
  • Switch to Local View.
  • Connect your local Git project to the workspace. [Local mocks]

2. Create a local mock server

  1. In the left sidebar, click the Items tab. [Local mocks]
  2. Click the Add (+) icon.
  3. Select Mock.
  4. Choose how to create it:
    • Create with template – basic HTTP server you can customize.
    • Create with AI – Agent Mode generates a mock from endpoints in your connected Git repo. [Local mocks]

Postman adds two files to your Git repo:

  • postman/mocks/mock-server-name.js – implementation (request/response logic).
  • postman/mocks/mock-server-name.json – configuration (port, routes, etc.). [Local mocks]

Commit these files so teammates can use the same local mock. [Local mocks]

3. Configure mock details (port, routes, etc.)

#
  1. In Items, select your local mock server.
  2. Click Edit mock configuration to open the mock-server-name.json file. [Configure details]
  3. Adjust fields such as:
    • protocol (http or https)
    • port
    • mockSrc (path to the JS implementation file)
    • routes
    • autoRestartOnChange
    • logging options. [Configure details]

4. Implement responses (using pm.mock and examples)

  1. In Items > Local mocks, select your local mock.
  2. Open mock-server-name.js.
  3. Replace placeholder IDs with real request/example IDs from your collections (click the placeholder strings to pick from a dropdown). [Customize implementation]

Example pattern:

const http = require("http");

const server = http.createServer((req, res) => {
  if (pm.mock.matchRequest('request-id', req)) {
    return pm.mock.sendExample('example-id', res);
  }

  // ...other routes/logic...
});
#
  • pm.mock.matchRequest() matches the incoming request to a saved Postman request (by method + path or request ID). [pm.mock]
  • pm.mock.sendExample() sends a saved example’s status, headers, and body as the response. [pm.mock]
  • You can also use pm.environment and pm.globals in this mock editor if needed. [pm methods in mock editor]

5. Start and use the local mock

  1. In Items, select the local mock server.
  2. Click Start. [Start local mock]
  3. Click Copy URL to copy the local mock URL. [Start local mock]
  4. Create an HTTP request in Collections, paste the mock URL, and send the request to see the simulated response. [Start local mock]
  5. Click Stop to stop the mock server when you’re done. [Start local mock]

If you tell me your desired base URL/port and what collection/examples you already have, I can help you map them into the local mock config and implementation.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: