Create a mock server from a collection in Postman
Create a mock server from the saved examples in a collection. When you send a request, Postman uses a matching algorithm to return the closest example’s response. This is the no-code path to a mock server, useful when you already have a collection of requests and example responses. For a step-by-step walkthrough, see the collection-based mock server tutorials.
Postman mock servers support collections that contain HTTP requests only. You can’t use a mock server with a collection that contains a non-HTTP request.
Create a mock server from a collection
-
In the sidebar, click
and select Mock Server.
-
Select Existing collection.
-
Configure your mock server details:
- Name — Enter a name for your mock server.
- Collection — The collection used for the mock server. Specify an existing collection, or select
Create collection to create a new one.
- Environment — (Optional) Select an environment to use environment variables with your mock server.
- Simulate a fixed network delay — Select a response delay or enter a custom delay. The mock server waits the specified time before sending the response.
- Save the mock server URL as a new environment variable — Save the mock server URL as a variable in a new environment so you can reference it in your requests.
- Make mock server private — Require a Postman API key in the request header when sending requests to the mock server. To learn more, see Make calls to a private mock server.
-
Click Deploy.
You can identify a mock server in the sidebar by the mock icon .
Create from request history
You can build a collection and mock server from requests in your Postman history.
- Click the
History tab in the sidebar.
- Click
View more actions next to a request and select Mock Request. You can also mock all requests for a specific date. Postman creates a new collection for the mocked requests.
- Configure the mock server’s name, environment, privacy, network delay, and whether to save the URL as an environment variable.
- Click Create Mock Server.
Call a mock server deployed from a collection
When you send a request to the mock server, Postman uses a matching algorithm to select the best saved example and return its response. You can provide multiple examples to handle different scenarios, and generate dynamic responses with variables and templates.
-
Click the
Services tab in the sidebar, then expand Mock Servers.
-
Select the mock server, then click
Copy Mock Server URL.
-
Open a new request and enter the mock server’s URL, including the path to the request you want to mock. For example,
https://4bb57fc2-219e-421e-86b4-4ffda6bf1b3b.mock.pstmn.io/get.Make sure the request you want to mock has at least one saved example. Learn more about adding examples to a request.
-
If the mock server is private, add your Postman API key to the request as an
x-api-keyheader. -
Click Send.
If you configured a delay for your mock server, Postman waits the specified period of time before sending the response.
Make calls to a private mock server
To make calls to a private mock server, add a Postman API key in the request header (for example, x-api-key:<Your-Postman-API-key>). If you share the collection, others can use their own Postman API keys to make calls to the mock server.
Use HTTP access control for a mock server
In addition to calling mock endpoints from Postman, you can make requests to a mock server from a web browser. Cross-Origin Resource Sharing (CORS) is enabled for Postman mock servers, so you can stub your web apps with mocked data. Development or production web apps can make requests to your Postman mock endpoint and receive example responses.
Configure a collection mock server
You can change the configuration for a mock server at any time.
-
Click the
Services tab in the sidebar, then expand Mock Servers.
-
Select a mock server and click
Edit Configuration.
-
Change the mock server’s name, environment, network delay, or privacy setting. You can also specify options for response matching.
You can’t change the mock server’s collection. To mock a different collection, create a new mock server.
-
Click Update Mock Server.
Match request body and headers
By default, the matching algorithm doesn’t consider the request’s body or headers when selecting the best response. You can change this in the mock server’s configuration to specify the exact response you want by matching the body or headers of the saved example.
-
Click the
Services tab in the sidebar, click Mock Servers, select a mock server, then click
Edit Configuration.
-
Under Response Matching, select the matching options you want to use:
- Request body — The mock server matches the request’s body to the body of the saved examples.
- Headers — The mock server matches the request’s headers to the headers of the saved examples. Add a comma-separated list of the header keys to match. Header matching isn’t case-sensitive.
-
Click Update Mock Server.
If you enable request body matching, you must add the Content-Type header to your examples and use the same value as your request, such as application/json. To learn how matching works, see How a Postman mock server matches requests to saved examples.
Delete a mock server
To delete a mock server, click the Services tab in the sidebar and choose Mock Servers, then click
View more actions next to the mock server’s name and click Delete.
View the call log
View and search the details of calls to your mock server using the call log. To view the call log, expand Mock Servers in the Services tab and select a mock server.
The call log displays a list of calls made to the mock server URL. Each entry shows the time the request was sent, the request method and path, and a response overview. With the call log, you can:
- Enter text in the search box to find specific requests.
- Click
Refresh Logs to view the most recent requests.
- Select an entry for more details about the request and response headers and body.
- Click
Copy Mock Server URL to copy the mock server’s URL.
Troubleshoot mock calls
Use the call log to troubleshoot your requests to mock servers.
If No matching requests displays in the Response column, there might be a problem with your mock server configuration. Make sure there’s an example saved for the request in the collection you’re mocking.
In the case of a service outage, you’ll get an HTTP 502, 503, or 504 response. Check the Postman status page for updates.