Mock servers simulate an API server by returning predefined data, so you can develop or test an API before it's ready for production. In Postman, mock servers use examples saved in an HTTP collection to return mock data.
In this tutorial, you'll learn how mock servers and examples work together and how to integrate them into your API workflow.
To get started, you'll need to create a collection and an example response.
https://postman-echo.com/get?test=123
as the request URL.Save the response you received from the Postman Echo service as an example.
Next, create a mock server for the C1 collection.
Click Mock servers in the sidebar.
Create a mock server for the C1 collection. For this tutorial, name the mock server "M1".
Click Save to save the mock server.
Click Copy URL to copy the "M1" mock server's URL.
In the C1 collection, create a new request.
Paste the mock URL as the request's URL, then click Send.
The request returns an error because the mock server's URL doesn't have a defined path. The path appended to the end of the mock server URL must match the path in the example's request URL (/get
). The method selected in the mock server and example must also match. Responses returned by the mock service depend on the URL and method in your saved examples.
To send a request to the mock server, add /get
to the end of the mock server's URL, then click Send.
You can add another example to test how responses from the mock service depend on saved examples.
Create and send a request. Select the GET method, then enter https://postman-echo.com/test
as the request URL.
The GET request to https://postman-echo.com/test
returns an HTTP 404 Not Found status code.
Save the request to the C1 collection.
Save the response as an example. Rename the example to "E2".
Send a request to the mock server. Update the path at the end of the mock server URL to /test
and click Send. The mock server returns the expected HTTP 404 Not Found status code.
Your examples can vary depending on the URL endpoint, request method, or status code. If you have multiple examples, you can choose to save each example under a unique endpoint URL, like you saw in this demonstration with
/get
and/test
. If you've saved examples with different response status codes, you can send an authenticated request to the mock server along with thex-mock-response-code
header specifying which integer response code your returned response needs to match.
Postman's mock service uses a matching algorithm to select the best saved example to return in response to a request. As part of this algorithm, the mock server looks at the request's path and query parameters, and then matches the request to a saved example. If you have examples with the same request path but different query parameters, you can mock different responses for different query parameters on the same request path.
The following scenario demonstrates how the matching algorithm selects the best saved example:
A mocked collection has one request named Request1 with two examples named Example1 and Example2.
In Example1, the id
parameter has a 1
value.
In Example2, the id
parameter has a 5
value.
In this scenario, Example1 and Example2 have the same request path (/get
), but they each have an id
query parameter with different values. When you send a request to the mock server URL and pass these different query parameters, Postman returns the exact response that matches both the path and the query parameters.
If no exact match is found, Postman returns the best response based on its matching algorithm.
Postman's mock service enables you to mock GraphQL queries. To mock GraphQL queries, make a request to the mock server using the request path and request body saved in the examples in the mocked collection.
Make sure to set the Content-type: application/json
header to in your examples.
Last modified: 2025/06/20