Create API mocks with JavaScript in Postman
A mock is a code-based stand-in for an API that you build with custom JavaScript request handlers. Unlike a mock server created from a collection that returns saved examples, a mock lets you define how requests are handled. A mock can return dynamic responses, keep state across requests, and reproduce complex API behavior without calling real services. This makes it useful for standing in for your own API before it’s built, or for an external service your app depends on that’s unavailable, still in development, or that you don’t want to call for real.
You, or a coding agent working in your repository, can build a mock from a specification, a collection, or your codebase. Start the mock locally to develop against it, or deploy it as a mock server to make it always available to your team. When your app depends on more than one service, group several mocks into a simulation to stand in for the whole set at once, then make them fail together to see how your service responds.
Mocks are available in Local View for all plans, and in Cloud View for Solo, Team, and Enterprise plans. For more information, see the pricing page.
Create a mock
Create a mock directly in Postman, generate one from a specification or collection, or use Agent Mode to build one from your context.
Learn more at Create a mock.
Customize a mock
Edit the mock’s JavaScript to control how it handles requests and returns responses. Use pm objects like pm.mock, pm.state, and pm.datasets to serve saved examples, persist data across requests, and return query-driven data.
Learn more at Customize a mock.
Scenarios and behaviors
Add named scenarios to model the different situations a single mock might encounter, then apply behaviors like latency, errors, and rate limits to test how your service handles a struggling dependency.
Learn more at Scenarios and behaviors.
Run a mock
Start a mock on your machine to develop against it, and inspect the logs, state, and run history it records as it handles requests.
Learn more at Run a mock locally.