Simulate a back end with Postman's mock service

Throughout the development process, delays on the front end or back end can hold up dependent teams from completing their work efficiently.  Some API engineers already use Postman to work on endpoints without relying on a front-end UI to simulate API requests.

Now, Postman has developed a new mock service to enable teams to simulate a backend server.  Front-end developers can simulate each endpoint in a Postman Collection (and corresponding environment) to view the potential responses, without actually spinning up a back end.

Front-end, back-end and API teams can now work in parallel, freeing up developers who were previously delayed as a result of these dependencies. Let’s walk through this step by step.

Set up a Collection for mocking

In this example, we have a Collection testAPI with corresponding environment testAPIEnv.  Let's set up a mock service to enable your front-end team to simulate each endpoint in testAPI and view the various responses.

Navigate to every request in the Collection testAPI that you would like to include in this simulation, and save responses with details about the response body, header or status codes that you would like to see returned by that endpoint. In this example, we will save 2 responses with status codes of 200 and 401 for this particular request.  Once you save the desired responses, the Collection is ready for mocking.

Retrieve information needed for mock creation

Let's retrieve the collectionId of testAPI using the Postman Pro API. Get a list of all your Collections using the GET All Collections endpoint. Search for the name of your Collection and retrieve the uid from the results, which will be used as the collectionId in the next step.

You can also use the Postman app to retrieve the collectionId. Find the Collection in your app and hit View Docs. The collectionId is visible in the documentation url: https://documenter.getpostman.com/collection/view/{{collectionId}}

As an optional step, you can include an environment template as a part of your simulation by retrieving the environmentId of testAPIEnv using the Postman Pro API. Get a list of all your environments using the GET All Environments endpoint. Search for the name of your environment and retrieve the uid from the results, which will be used as the environmentId in the next step.

Create a mock using the Postman Pro API

Create a mock using the POST Create Mock endpoint with the collectionId and environmentId you retrieved previously.

Verify that the mock has been created using the GET All Mocks endpoint, and your Collection is now ready to be simulated.

Run the mock service

Mock your Collection using the following url: https://{{mockId}}.mock.pstmn.io/{{mockPath}}

      • mockId is the id that you received upon creating the mock and can be retrieved using the GET All Mocks endpoint.
      • mockPath is the path of your request that you’d like to mock, for example api/response.

Add the request header(s):

  • Mock requests require one mandatory header, x-api-key, which is your Postman Pro API key for authentication. Don't have a Postman Pro API key? Create one here.
  • Mock requests also accept another optional header, x-mock-response-code, which specifies which integer response code your returned response should match.  For example, 500 will return only a 500 response. If this header is not provided, the closest match of any response code will be returned.

And there we have it. We have walked through the entire process of mocking a Collection from start to finish. We hope that you find this feature useful in increasing the efficiency of your team collaboration. And as always please reach out to us if you have any comments or questions.

22 thoughts on “Simulate a back end with Postman's mock service”

  1. As I understood, I need to have the service already running in order to be able to save the response, why would I need a mock service in this case? It would be useful to have the ability to create raw responses and save them.

    1. Hello,
      I apologize for the delay in response. We have developed a feature that will enable you to create custom responses and will be releasing it very soon. Please stay tuned for updates!
      Anuhya

  2. Im really loving Postman and have been looking forward to seeing some mocking function. Im not sure is I understand the tutorial fully though. I can create a the collections, environments and mock endpoints. But how do I edit responses to be saved ?

    Say that Im going to build an api with endpoint /customers but I dont already have one – i cant generate the proper response to be saved for the mock endpoint.

    1. Hello Jon,
      I apologize for the delay in response. We have developed a feature that enables you to create custom responses and will be releasing it very soon. Please stay tuned for updates!
      Anuhya

  3. Hello. This feature is very interesting for our company, but I got fail in investigation.
    Thank you for this tutorial, but I need your help with last step. It isn't clear for me. I created Mock Service and got "mockUrl": "https://{{id}}.mock.pstmn.io". But I don't understand how I can simulate following request "https://{{url}}/{{my_path}}" ?
    And I have one more question regarding "saving responses". I understood correct that if server doesn't work that I cannot save response ?
    I will be very grateful for the answer.

    1. Hello Alex,
      Could you please email help@getpostman.com with the collection uuid, environment uuid, mock id, and mock path that you're trying to mock? I can help you with the mocking process and with understanding how saved responses work. We have already developed and will be releasing a feature very soon which will allow you to create a custom response without the need of a backend.
      Thanks!
      Anuhya

  4. Hello.
    So, we cannot save response without server? Will be better if we can create response in from typing according docs (without sever).
    Waiting your answer on this and previous questions.
    Thanks advance

    1. Hello Alex,
      Thanks for reaching out! We are, in fact, in the process of developing a way for you to create custom saved responses without a backend. This feature is already in development and will be released very soon. Stay tuned for updates!
      Anuhya

  5. Hello There, This particular page, isn't clear on how to create a mock service. I have understood how to create a mock API URL and get one, but unfortunately didn't understood on how to set the mock response, response code to this newly created mock API (example: https://dc1d5934-41d4-4b10-93b3-49c7c3981959.mock.pstmn.io/{{myactualpath}})

    Would really appreciate if someone could help in here.

    1. Hello Veera,
      Thanks for reaching out! Could you please email help@getpostman.com with the collection uuid, environment uuid, mock id, and mock path that you're trying to mock?
      I can also walk you through the entire process of mocking :).
      Looking forward to connecting,
      Anuhya

  6. I followed the exact steps and managed to create a mock but I could not call it. Getting the following error
    {
    "name": "mockNotFoundError",
    "message": "Mock not Found."
    }
    I believe it might be related to {{mockPath}} I couldn't figure out how to get it. I used the path in the original call but didn't work

    1. Hello Joseph,
      Thanks for reaching out! Could you please email help@getpostman.com with the collection uuid, environment uuid, mock id, and mock path that you're trying to mock?
      Looking forward to connecting,
      Anuhya

    1. Hello Andrey,
      Thanks for writing to us. Could you email help@getpostman.com with your mockId, collectionId, environmentId and the url you are trying to hit? I can help you debug and walk you through the process of forming the url for mocking, including what and how to use the pathUrl.
      Thanks,
      Anuhya

    1. Hello,
      Thanks for writing to us. So we actually do have this on our roadmap: https://trello.com/c/Nj8QV6J7/38-add-request-examples. Saved responses will now be renamed to Examples as we move forward. I understand that it is difficult to understand that from the title of the card. We will work on making these cards more explicit and self-explanatory. This feature will be out very soon.
      Please let us know if there's anything else we can help with!
      Anuhya

    1. Hello Vishnu,
      The responses that are returned from the Mock Service are the saved Examples that you create in the collection you're mocking. You can see more on how to do that here. You can also use the Postman Pro API to save Examples that can be mocked for a collection. This is not as straightforward, but can be done. If you would like more information on using the PRO API to save examples, shoot us an email at help@getpostman.com.
      Hope that answered your question!
      Anuhya

Leave a Reply

Your email address will not be published. Required fields are marked *