#Local Mocks
5 messages · Page 1 of 1 (latest)
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
- In the left sidebar, click the Items tab. [Local mocks]
- Click the Add (+) icon.
- Select Mock.
- 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.)
- In Items, select your local mock server.
- Click Edit mock configuration to open the
mock-server-name.jsonfile. [Configure details] - Adjust fields such as:
protocol(httporhttps)portmockSrc(path to the JS implementation file)routesautoRestartOnChangeloggingoptions. [Configure details]
4. Implement responses (using pm.mock and examples)
- In Items > Local mocks, select your local mock.
- Open
mock-server-name.js. - 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.environmentandpm.globalsin this mock editor if needed. [pm methods in mock editor]
5. Start and use the local mock
- In Items, select the local mock server.
- Click Start. [Start local mock]
- Click Copy URL to copy the local mock URL. [Start local mock]
- Create an HTTP request in Collections, paste the mock URL, and send the request to see the simulated response. [Start local mock]
- Click Stop to stop the mock server when you’re done. [Start local mock]