> This page is for Postman Docs, version Latest (v12) (default).
> For other versions, use one of these documentation indexes:
> - Latest (v12) (default): https://learning.postman.com/latest-v-12/llms.txt
> - v11: https://learning.postman.com/v11/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://learning.postman.com/llms.txt.

# Create API mocks with JavaScript in Postman

> Build a code-based stand-in for an API with custom JavaScript request handlers, for your own API or a service your app depends on.

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](/docs/design-apis/mock-apis/mock-servers/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](/docs/design-apis/mock-apis/mock-servers/from-a-mock) to make it always available to your team. When your app depends on more than one service, group several mocks into a [simulation](/docs/design-apis/simulate-conditions/overview) to stand in for the whole set at once, then make them fail together to see how your service responds.

> **Info**
>
> 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](https://www.postman.com/pricing/).

## Create a mock \[#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](/docs/design-apis/mock-apis/mocks/create-a-mock).

## Customize 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](/docs/design-apis/mock-apis/mocks/customize-a-mock).

## Scenarios and behaviors \[#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](/docs/design-apis/mock-apis/mocks/scenarios-and-behaviors).

## Run a mock \[#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](/docs/design-apis/mock-apis/mocks/run-a-mock).