Deploy a mock server

View as Markdown

Create a mock server in Postman to simulate a real API server that’s always available to handle requests. You can create a mock server from a mock or from a collection.

To view mock servers in your workspace, click the Services icon Services tab in the sidebar, then expand Mock Servers.

Mock servers can be private or public. Private mock servers require a Postman API key in the request header. Public mock servers can receive requests from anyone and anywhere (such as a browser, application code, or a curl command). New mock servers are public by default. Super Admins and Admins can enable or prohibit users from creating public mock servers. See Manage mock servers to learn more.

Postman mock servers only support HTTP collections. You can’t use mock servers with non-HTTP collections.

Create from a mock

Deploy a mock as a mock server to run it as an always-available cloud-hosted service. Once deployed, anyone with access can send requests to it at any time.

To create a mock server from an existing mock, do the following:

  1. In the sidebar, click Add icon and select Mock Server.

  2. Select Existing mock. This is selected by default.

  3. Configure your mock server details:

    • Name — Enter a name for the mock server.
    • Mock — Select a mock that hasn’t been deployed as a mock server yet. You can also select Add icon Create mock to create a new mock. To learn more, see Build API mocks with JavaScript.
    • URL — (Optional) Customize the URL for your mock server. The customized URL must be unique and can only have lowercase letters, numbers, and hyphens.
    • Allow public access — Select this checkbox to allow anyone with the mock server URL to send requests to the mock server. To learn more, see Allow public access.
    • Enable sessions — By default, mock servers maintain separate state for each session. Clear this checkbox to share a single state store across all requests instead. To learn more, see State sessions.
    • Auto deploy when mock changes — Select this option to automatically redeploy the mock server when you make changes to the mock.
  4. Click Deploy.

You can also create a mock, start it locally to test it, and then deploy it as a mock server when you’re ready. To learn more, see Build API mocks with JavaScript.

Make a mock server public

Mock servers deployed from a mock are private by default. Private mock servers can only be accessed by workspace members or callers that include a valid x-api-key header with a Postman API key.

To allow anyone with the mock server URL to send requests to the mock server, select Allow public access when creating or editing the mock server.

On Team and Enterprise plans, your team may require Team Admin approval before a mock server can be made public. If approval is required, your request will be submitted for review and the mock server will remain private until it’s approved.

State sessions

By default, all requests to a mock server share the same state store when using pm.state. When you enable sessions, each session gets its own isolated copy of state.

Use state sessions to simulate multiple users, run parallel tests, or test stateful workflows without requests affecting each other’s data. Identify a session using the x-mock-session header. Postman can also use a pm_mock_session cookie to maintain a session across requests automatically.

To learn more about pm.state, see Use pm methods in the mock editor.

Configure a deployed mock server

  1. Click the Services icon Services tab in the sidebar.

  2. Click Mock Servers.

  3. Select the mock server you want to configure.

  4. In the upper right of the editor, select Options icon > Edit configuration.

  5. You can configure the following details:

    • URL — The unique URL for the mock server. It can only have lowercase letters, numbers, and hyphens.
    • Allow public access — Allow anyone with the mock server URL to send requests to it. On Team and Enterprise plans, this may require Team Admin approval.
    • Enable sessions — When turned on, each session gets its own isolated copy of state. When turned off, all requests share a single state store.
    • Auto deploy when mock changes — Automatically redeploy the mock server when you make changes to the mock.
  6. Click Save.

Create from a collection

Create a mock server from saved examples in a collection to help with API development and testing. When you send a request to the mock server, Postman matches the request to a saved example in your collection and responds with the data in the example.

To create a mock server from an existing collection, do the following:

  1. In the sidebar, click Add icon and select Mock Server.

  2. Select Existing collection.

  3. Configure your mock server details:

    • Name — Enter a name for your mock server.

    • Collection — The collection used for the mock server. You can specify a collection if you selected an existing collection, or select Add icon Create collection to create a new collection and add it to the mock server.

    • 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 — Select this option to save the mock server URL as a variable in a new environment. You can then reference the variable in your requests. Learn more about using variables with mock servers.

    • Make mock server private — Select this option to make your mock server private. You must specify an API key in the request header when sending requests to a private mock server. Learn more about making calls to a private mock server.

      If the Make mock server private option is unavailable, it may be turned off in Team resources. An Admin or Super Admin can turn it back on.

  4. Click Deploy.

    Postman displays the details you need to use the mock server. To get these details at any time, click the Services icon Services tab in the sidebar, then expand Mock Servers and select the mock server.

  5. Click Copy icon Copy Mock Server URL in the workbench to begin making calls to your mock server.

You can identify a mock server in the sidebar by the mock icon Mock icon.

Edit the mock server configuration

You can change the configuration for a mock server at any time.

  1. Click the Services icon Services tab in the sidebar, then expand Mock Servers.

  2. Select a mock server and click Setting icon Edit Configuration.

  3. Make any changes to the mock server configuration. You can change the mock server’s name, environment, network delay, and privacy setting. You can also specify options for response matching.

    You can’t change the mock server’s collection. If you need to mock a different collection, create a new mock server.

  4. When you are done making configuration changes, click Update Mock Server.

Match request body and headers

When you send a request to the mock server, Postman uses a matching algorithm to decide which example to return in a response.

By default, the matching algorithm doesn’t consider the request’s body or headers when selecting the best response to return. You can change this behavior in the mock server’s configuration. Using body or header matching, you can specify the exact response you want the mock server to return by matching the body or headers of the saved example.

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 use body or header matching with a mock server, do the following:

  1. Click the Services tab on the sidebar, then click Mock servers, select a mock server, and click Setting icon Edit Configuration.

  2. 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. In the box, add a comma-separated list of the header keys that you want the mock server to match. Header matching isn’t case-sensitive.
  3. Click Update Mock Server.

For more advanced request matching and dynamic response logic, use a mock.

Delete a mock server

To delete a mock server, click the Services tab on the sidebar and choose Mock servers, then click Options icon View more actions next to the mock server’s name and click Delete.

Create from history

You can build a collection and mock server based on requests from your Postman history.

  1. Click the History icon History tab in the sidebar.

  2. Click Options icon 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 request or requests.

  3. Configure the following details:

    • Name — Enter a name for your mock server.
    • Environment — (Optional) Select an environment to use environment variables with your mock server.
    • Make mock server private — Select this option to make your mock server private. You must specify an API key in the request header when sending requests to a private mock server. Learn more about making calls to a private mock server.
    • Save the mock server URL as a new environment variable — Select this option to save the mock server URL as a variable in a new environment. Learn more about using variables with mock servers.
    • Simulate a fixed network delay — Select a response delay or enter a custom delay.
  4. Click Create Mock Server.

To edit the configuration, match request body and headers, or delete the mock server, see Create from a collection.

Next steps

To learn more about mock servers, see the following resources: