Simulate your API using a Git-backed mock server
Simulate your API using a Git-backed mock server
Simulate your API using a Git-backed mock server
With Native Git, you can create a local mock server that’s stored in your local Git repository. A local mock server enables you to simulate a real API server locally in a branch, where you’re making API changes. This is useful for local API development and testing when dependencies are unavailable or still under development, and before committing your changes to the Postman cloud.
You can create a local mock server from a template that you can customize or using Agent Mode. You can start the mock server to simulate API responses based on the request and response logic defined in your implementation file, and view logs for APIs the mock server handles.
Before you get started, connect your Git project to your workspace.
Click the Items tab in the sidebar.
Click .
Select Mock and choose how you’d like to create your local mock server:
This adds two files to the postman directory in your local Git repository:
postman/mocks/mock-server-name.js - A JavaScript file that contains the mock server implementation. Set the request and response for simulating real API behavior.postman/mocks/mock-server-name.json - A JSON file that contains the mock server configuration, such as the port number and routes.Make sure to commit the JavaScript implementation file and JSON configuration file to your Git repository. This ensures your teammates get the local mock server files.
Define how your mock server handles requests and returns responses in the mock-server-name.js implementation file. This is where you simulate the behavior of a real API.
To customize your mock server, do the following:
Click the Items tab in the sidebar.
Click Mocks.
Select the local mock server you want to customize.
Update the implementation file with your request handling logic.
Example:
Local mock servers are implemented using custom request handlers. You can use standard HTTP server patterns to control how requests are handled. For example, you can add conditionals, generate dynamic data, or implement any valid JavaScript logic.
Within these handlers, you can:
pm.mock to serve responses from your saved examples.pm.state to persist data across requests and enable stateful behavior.pm.datasets to query datasets and use the data in your request handling logic.pm.test and pm.expect to add test assertions and validate request or response data.Learn more about the pm objects available in the mock editor.
Configure the mock server details in the mock-server-name.json file, such as the port number and routes.
To configure your mock server details, do the following:
Click the Items tab in the sidebar.
Select the local mock server you want to configure.
Click Edit mock configuration in the upper right to open the JSON file.
You can update the following details:
version - The version of the mock server configuration file.name - The name of the mock server.protocol - The protocol the mock server uses, either http or https.port - The port number the mock server listens on.mockSrc - The path to the JavaScript implementation file.routes - An array of route objects that define the endpoints for the mock server.autoRestartOnChange - A Boolean value that determines whether the mock server should automatically restart when changes are made to the configuration or implementation files.logging - An object that contains logging configuration for the mock server, such as the log level and Boolean whether to show the body of requests and responses in the logs.With Agent Mode, you can generate and modify local mock servers using prompts. It understands your workspace context and can create realistic mock behavior, including multi-step workflows and data that persists across requests.
The following are example prompts you can use to create and test different API behaviors with local mock servers.
Use this prompt to create a mock server that introduces failures at specific intervals.
Use this prompt to mock a basic login and authorization flow where access depends on prior requests.
Use this prompt to create a mock service that stores and returns data across requests.
Use this prompt to ensure repeated requests return consistent results instead of creating duplicates.
Use this prompt to model flows where later requests depend on earlier ones.
Use this prompt to create a mock that reflects updates made during execution.
Use this prompt to create behavior that changes based on prior interactions with specific resources.
Use this prompt to model APIs that return different states over time.
You can use some pm objects in the mock editor the same way you use them in scripts.
Postman provides an object called pm.mock that’s available in the mocks sandbox environment. It gives you structured, Postman-aware functions for matching incoming requests and sending responses. This includes the ability to serve responses from your existing saved Postman examples rather than hard-coding everything. To learn more, see Reference requests and examples in local mock servers.
The pm.state object is available as a beta feature.
Postman provides an object called pm.state that’s available in the mocks sandbox environment. It provides a persistent store for managing data across requests, enabling your mock server to behave like a real service instead of returning static responses. State persists across runs until it’s cleared using pm.state.clear(). To learn more, see Persist state across requests in local mock servers.
The pm.datasets function is available as a beta feature.
Postman provides a function called pm.datasets that enables you to access datasets and run SQL queries against them. You can run predefined views or custom queries to retrieve structured data at runtime. This enables you to use persistent, queryable data across requests, making your mock servers more dynamic and realistic. To learn more, see Manage and use datasets in scripts.
The pm.test object enables you to define test assertions in your mock server implementation file. You can use it with pm.expect, which provides Chai-style expectations for validating request headers, body content, status codes, or custom variables. Test results (pass, fail, or skip) are recorded and surfaced in mock server logs. Learn more about writing tests using pm.test and pm.expect.
The pm.environment object provides access to variables in the active environment. To learn more, see Reference variables in Postman scripts.
The pm.globals object provides access to global variables at the workspace level. To learn more, see Reference variables in Postman scripts.
Click Stop in the upper right to stop the mock server.
Apply a simulation to a local mock server to introduce real-world conditions like latency, errors, and rate limits. This enables you to observe how your service behaves under stress and performance constraints, so you can address issues before deploying to staging or production.
From a mock server, you can click Simulate in the upper right to create or update a simulation.
To learn more, see Simulate real-world conditions in Postman.
View details about incoming requests to your local mock server and the responses sent in the logs.
You can click a request to view more details about the request and response, such as the headers and body.
Logs with the simulator icon indicate requests that ran with a simulation applied.
Click Clear logs to clear the logs for the mock server.