Skip to navigation

Postman CLI mock commands (postman mock)

View as Markdown

This topic covers mock server commands for the Postman CLI.

Many of these commands work with both local mocks and cloud mocks. Local mocks are identified by a file path, and cloud mocks are identified by a mock ID. When you pass a mock ID, the command acts on the mock in the Postman Cloud, which requires you to sign in with the postman login command or pass an --api-key.

postman mock run

You can use the postman mock run command to start a mock, which is a code-based JavaScript stand-in for an API. Provide a path to a local mock, or a cloud mock’s ID. When you provide a cloud mock ID, the CLI fetches that mock from Postman and runs it on your machine. This command doesn’t operate a mock server hosted in the Postman Cloud. To publish a cloud mock to a hosted mock server, use the postman mock deploy command.

While the mock runs, the CLI prints a live log of each request and response it serves, matching the detail of postman mock log: headers, bodies, and timing, with credentials redacted. Pass --output ndjson for a machine-readable event stream instead, with one JSON object per line.

A config.yaml file lets you configure details like the mock’s port number, name, script, and more. The mock runs on the specified port until you stop it. If the specified port is already in use, the mock falls back to an available port assigned by the OS. Make sure you also have a JavaScript file with an HTTP server that sets the request and example response for simulating real API behavior.

You can add the command to your CI/CD script to start a mock as a dependency for your application or test suite. Once running, your service or external tests can send requests to it as if it were a real API. This enables you to simulate API behavior for testing and development.

By default, the command records eligible runs in the mock’s start history, so CLI and CI runs appear in Postman alongside runs started from the Postman app. A run started from a cloud mock ID is recorded automatically unless you pass --no-history.

Learn more at Create mocks with JavaScript.

To run a collection against a mock, use the --use-mock option with the postman collection run command.

Usage

postman mock run <mock-id-or-path>
<mock-id-or-path>

A cloud mock’s ID, or a path to a local mock. The path can be a mock’s config.yaml file, a mock directory such as postman/mocks/<slug>, or a .js handler script.

Options

-e, --environment <path>

Specifies the path to an environment file (JSON or YAML) loaded into pm.environment for the mock’s scripts. The file can be a Postman environment (with a values array) or a flat map of key: value pairs.

-g, --globals <path>

Specifies the path to a globals file (JSON or YAML) loaded into pm.globals for the mock’s scripts.

--dataset <path-or-dir>

Loads one or more local .dataset.yaml files, or directories of them, so the mock’s request handlers can read them with pm.datasets(<id>). Pass --dataset more than once to load multiple datasets. The datasets load before the mock starts.

-p, --port <port>

The port the mock listens on. Pass auto for an OS-assigned ephemeral port. By default, the mock uses the port from its configuration, falling back to an ephemeral port if that port is busy.

--api-key <key>

Your Postman API key, used to authenticate when you run a cloud mock by its ID. Defaults to your postman login session.

-w, --workspace <id>

The workspace that owns the run, used to record its start history. Auto-resolved when you run a cloud mock by its ID. Required to record the history of a run started from a local path.

--no-history

Don’t record this run in the mock’s start history.

--output <format>
'auto' | 'ndjson'Defaults to auto

The live output format. Use auto to print human-readable request and response logs. Use ndjson to stream one JSON event per line on stdout instead, for machine-readable output. Each request event carries the status code, duration, response name and size, any applied scenarios, and any error.

--verbose

Prints request and response headers and bodies for each request. By default, each request prints one line, plus any applied scenario or error. With --output ndjson, this adds the headers and bodies to each request event, along with an error stack when one is available.

Example

postman mock run mock-config.yaml
postman mock run mock-config.yaml --environment ./postman/environments/dev.environment.yaml
# Record start history for a run started from a local path
postman mock run ./postman/mocks/orders --workspace 12345678-90ab-cdef-1234-567890abcdef

postman mock generate

You can use the postman mock generate command to generate a runnable mock from a Postman Collection, an OpenAPI 3.0 or 3.1 specification, or a dataset. The command auto-detects the type of a source file. It accepts a collection in v2.0, v2.1, or v3 format, either as a file or a Git-native v3 collection directory, or an OpenAPI specification file. To generate from a specification, collection, or dataset in Postman instead, use the --spec, --collection, or --dataset option.

If you run the command without a source, it scaffolds a sample shopping-cart mock (POST /cart/items, GET /cart, POST /checkout) that shows how to serve saved examples, which you can use as a starting point.

After you generate a mock, start it with the postman mock run command. To create the mock in a Postman workspace, use the --workspace option.

If AI can’t build an ai mock, for example because it times out, the command builds a stateful mock instead and warns with the reason and a hint. If the AI’s rules reach only some operations, the warning lists the operations they didn’t reach. Both cases exit with code 0, unless you pass --strict. A command line that doesn’t parse, such as --port abc or an unknown option, exits with code 2. The command checks input before it sends any request, so it rejects malformed IDs, empty options, and --instructions text over 2,000 characters.

Learn more at Create mocks with JavaScript.

Usage

postman mock generate [source-path] [options]
[source-path]

The path to the source collection or OpenAPI specification file. The source type is auto-detected. If omitted, the command generates a sample mock.

Options

-o, --output <dir>
Defaults to postman/mocks/<slug>

The output directory for the generated mock. You can’t use this option with --update.

--name <name>

A display name for the generated mock. With a source, this defaults to the collection’s or specification’s own name. Required when [source-path] is omitted, since the sample mock has no source name to default to. With --update, renames the mock.

--port <port>
Defaults to 4500

The port the mock listens on. With --update, changes the mock’s port.

--mode <mode>
'static' | 'stateful' | 'ai'Defaults to static

How the mock behaves:

  • static — (Default) Replays the examples.
  • stateful — Keeps records, with handlers for create, read, list, update, and delete operations.
  • ai — Generates a stateful mock with realistic data and business rules, inferred from the specification, collection, or dataset you give it. Postman generates it, so you must sign in with the postman login command.
--stateful

The same as --mode stateful. Generates handlers that store data with pm.state for endpoints that map to create, read, list, update, and delete operations, instead of returning static example responses. Other endpoints keep static responses. An endpoint whose resource has no create endpoint also keeps a static response. For a local file, the command generates the handlers on your machine. With --workspace, --spec, --collection, or --dataset, Postman generates them and validates them with seed data. You can’t combine --stateful with a --mode other than stateful.

--instructions <text>

Used with --mode ai, rules the source doesn’t state, such as "Orders can only be canceled before they ship". Without this option, the AI works from the source alone. Instructions can be up to 2,000 characters, and the command refuses longer text before it sends anything. The mock records the instructions, so --update reuses them.

--strict

Used with --mode ai, fails instead of falling back to a mock built without AI. If Postman would build the mock without AI, the command refuses it, exits with code 1, and writes or saves nothing. The error’s errorCode names the reason. If the AI’s rules reach only some operations, the command keeps the mock and exits with code 4. You can’t use this option with any other --mode.

--spec <specId>

Generates the mock from a specification in Postman, by ID, instead of a local file. With --workspace, the mock stays linked to the specification and regenerates when the specification changes.

--collection <collectionId>

Generates the mock from a collection in Postman, by ID, instead of a local file. With --workspace, the mock stays linked to the collection and regenerates when the collection changes.

--dataset <datasetIdOrPath>

Generates the mock from a dataset. Each table becomes a resource, and its first 20 rows become the records. Pass the ID of a dataset in Postman, or the path to a local .dataset.yaml file with CSV and JSON sources. Postman generates the mock in stateful mode by default, or in ai mode. In ai mode, the AI sees the column names and types, never the rows.

--force

Overwrites the config.yaml or default.js file in the output directory if the directory already holds a mock. Other files are kept. A non-empty directory with no mock in it is never written to, with or without --force.

-u, --update <mock-path-or-id>

Updates an existing mock in place from the source. Provide the path to a local mock’s config.yaml file, or the directory that contains it. This regenerates the default scenario handler and preserves the existing scenarios. It also preserves the name and port, unless you pass --name or --port. If you omit the source and --mode, the command uses the source, mode, and instructions recorded by the mock. Any source or --mode you do pass replaces what the mock recorded, so a later update that omits them uses the new values. You can’t use this option with --output.

To update a cloud mock, provide its ID. Postman regenerates the mock in the mode it’s linked to, with its recorded instructions, unless you pass --mode or --instructions. You can’t pass two sources. An AI mock isn’t regenerated each time its source changes, so run this command to refresh it.

-w, --workspace <id>

Creates the mock in the specified Postman workspace. You can’t use this option with --output, --force, or --update with a local mock path. With --deploy, it only selects the workspace to push to. With --update and a cloud mock ID, it names the workspace that holds the mock.

--deploy

Uploads the generated mock to the Postman Cloud and deploys it to a live mock server, without asking any questions. The server is private by default, so callers must send a Postman API key. Uses the workspace from --workspace, or the workspace linked to this project.

--public

Requires --deploy. Lets anyone with the link reach the mock server, instead of requiring a Postman API key.

-s, --slug <slug>

Used with --deploy, the short label used in the mock server’s web address. The command refuses this option without --deploy. Defaults to the mock’s name followed by six hexadecimal characters, such as orders-3f9c1a.

--json

Used with --deploy, prints the live server’s details as machine-readable data instead of a summary.

--timeout <ms>
Defaults to 75000

How long to wait for Postman to generate the mock, in milliseconds.

--api-key <key>

Your Postman API key, used for anything Postman generates, saves, or deploys. Defaults to your postman login session.

--json

Prints the result as machine-readable data instead of a summary. With --deploy, the result is the live server’s details. If the command fails, it prints nothing on stdout and writes { "error": "<message>", "code": <exit code> } to stderr. When Postman names the error, the output also includes errorCode, and hint when Postman suggests a fix. When Postman builds an AI mock without AI, ai includes fallbackReason and, if there is one, hint. When AI’s rules reach only some operations, aiCoverage lists them and the operations it dropped, and partial is true.

-x, --suppress-exit-code

Always exits with code 0, even on failure.

Examples

postman mock generate ./my-collection.json
postman mock generate ./openapi.yaml \
--output ./postman/mocks/api \
--port 4010
postman mock generate ./openapi.yaml --stateful
postman mock generate ./openapi.yaml \
--mode ai \
--instructions "Orders ship in 2 days"
postman mock generate --spec 12345678-90ab-cdef-1234-567890abcdef \
--mode stateful \
--workspace 12345678-90ab-cdef-1234-567890abcdef
postman mock generate --dataset ./postman/datasets/shop/shop.dataset.yaml
postman mock generate ./my-collection.json \
--update ./postman/mocks/orders
postman mock generate --update ./postman/mocks/orders
# Refresh a cloud AI mock after you edit its source
postman mock generate --update <cloud-mock-id>
# Fail instead of keeping a mock that AI only partly shaped
postman mock generate ./openapi.yaml --mode ai --strict --json

postman mock get

Use the postman mock get command to show a mock’s details. Provide the path to a mock, or a cloud mock’s ID. A path shows the mock’s details and run status. A cloud mock ID shows the mock’s cloud details, including its deployed mock servers and their IDs.

The summary shows the mock’s name in bold, followed by short lines of details such as its ID and URL. The output lists the endpoints for every scenario the mock defines, grouped by scenario, with the default scenario marked and an endpoint count per scenario.

For a mock that postman mock generate created, the output also includes a source row naming the specification, collection, or dataset it was built from and the mode it was built in. This is the source that --update reuses when you regenerate the mock without naming one.

Usage

postman mock get <mock-id-or-path>
<mock-id-or-path>

A cloud mock’s ID, or the path to a mock (or a directory that contains one).

Options

--json

Prints the mock details as machine-readable data instead of a summary. If the command fails, it prints nothing on stdout and writes { "error": "<message>", "code": <exit code> } to stderr.

--api-key <key>

Your Postman API key, used to authenticate when you get a cloud mock by its ID. Defaults to your postman login session.

Examples

postman mock get ./postman/mocks/orders
postman mock get ./postman/mocks/orders --json

postman mock list

Use the postman mock list command to list mocks. Listing is local-first. By default, it lists the local mocks in your repository’s postman/mocks directory. Provide a different path to search there instead, or pass the --workspace option to list a specific Postman Cloud workspace’s mocks instead.

Usage

postman mock list [path-or-directory]
[path-or-directory]

A directory to search for local mocks. Defaults to postman/mocks. You can’t combine it with --workspace.

Options

-w, --workspace <id>

Lists the mocks in the specified Postman Cloud workspace instead of local mocks. Listing is local-first, so this option is only needed to list cloud mocks. You can’t combine it with a path.

--api-key <key>

Your Postman API key, used to authenticate with the --workspace option. Defaults to your postman login session.

--json

Prints the mock list as machine-readable data instead of a table. If the command fails, it prints nothing on stdout and writes { "error": "<message>", "code": <exit code> } to stderr.

Examples

# Local mocks in your repository (postman/mocks)
postman mock list
# Local mocks under a specific directory (as JSON)
postman mock list ./postman/mocks --json
# Cloud mocks in a specific workspace
postman mock list --workspace 12345678-90ab-cdef-1234-567890abcdef

postman mock delete

Use the postman mock delete command to permanently remove a mock. Provide the path to a mock, or a cloud mock’s ID. A path removes the mock from your machine. A cloud mock ID removes it from the Postman Cloud.

Usage

postman mock delete <mock-id-or-path>
<mock-id-or-path>

A cloud mock’s ID, or the path to a mock (or a directory that contains one).

Options

-y, --yes

Skips the confirmation prompt.

--api-key <key>

Your Postman API key, used to authenticate when you delete a cloud mock by its ID. Defaults to your postman login session.

--json

Prints the result as machine-readable data. Requires --yes, because a confirmation prompt would break the JSON output. If the command fails, it prints nothing on stdout and writes { "error": "<message>", "code": <exit code> } to stderr.

Examples

postman mock delete ./postman/mocks/orders
postman mock delete ./postman/mocks/orders --yes

postman mock stop

Stops a mock that you started locally with postman mock run. The command returns after the mock exits and its port no longer accepts connections.

The mock shuts down the same way as when you press Ctrl+C: it closes its start history and emits its summary event. If the mock is still running after --timeout, the command force-stops it. Stopping a mock that isn’t running succeeds.

Usage

postman mock stop <mock-id-or-path> [options]
<mock-id-or-path>
Required

The cloud mock ID or path that you passed to postman mock run. A path can be a mock directory, a config.yaml or .yaml manifest, or a .js handler.

Options

--timeout <seconds>

The number of seconds to wait for the mock to shut down before force-stopping it. Defaults to 15.

--json

Prints the result as machine-readable data: wasRunning, forced (whether the mock had to be force-stopped), pid, and port. If the command fails, it prints nothing on stdout and writes { "error": "<message>", "code": <exit code> } to stderr.

Examples

postman mock stop ./postman/mocks/orders
postman mock stop 12345678-90ab-cdef-1234-567890abcdef
postman mock stop ./postman/mocks/orders --timeout 5

postman mock push

This command uploads a mock from your repository to Postman, creating it as a cloud mock. The first push creates the cloud mock. Each later push updates that same mock instead of creating a duplicate, so your repository stays the source of truth.

The push also stores the mock’s name, port, origin, and interceptRequests settings. Postman stores up to 20 public, fully qualified origin hosts. It leaves out others, such as localhost, private or loopback IP addresses, and cloud metadata names, and warns about each one it leaves out. An empty origin: [] clears the stored origins. If the cloud mock that the mock recorded was deleted, the command creates a new one and updates .postman/resources.yaml. Other 404 errors still fail.

Uploading a mock doesn’t start a mock server. To turn the uploaded mock into a live mock server, run postman mock deploy.

To use this command, sign in to Postman with the postman login command, or pass an --api-key.

Usage

postman mock push <mockPath> [options]
<mockPath>

The path to the mock in your repository, for example ./postman/mocks/orders.

Options

-w, --workspace <id>

The Postman workspace to upload the mock to, by ID. Defaults to the workspace already linked to your project.

--api-key <key>

Your Postman API key. If omitted, the command uses your postman login session.

--json

Prints the result as machine-readable data: mockId, name, created, deployed, workspaceId, and postmanUrl. If the command fails, it prints nothing on stdout and writes { "error": "<message>", "code": <exit code> } to stderr.

Examples

postman mock push ./postman/mocks/orders
postman mock push ./postman/mocks/orders --workspace 12345678-90ab-cdef-1234-567890abcdef

postman mock deploy

You can use the postman mock deploy command to publish a cloud mock to a mock server, making it available at a hosted URL. This command works with cloud mocks only, so provide the mock’s ID rather than a local path.

Run the command interactively to set the mock server’s name, slug, and visibility, or pass those as options to deploy from a script. New mock servers are private by default. To call a private mock server, send a valid Postman API key in the x-api-key header. After a private deploy, the CLI prints an example request showing this.

The postman mock deploy command is available on Postman Solo, Team, and Enterprise plans. For more information, see the pricing page.

To use this command, sign in to Postman with the postman login command, or pass an --api-key.

Usage

postman mock deploy <mock-id> [options]
<mock-id>

The ID of the cloud mock to deploy.

Options

-n, --name <name>

A display name for the mock server.

-s, --slug <slug>

The subdomain for the mock server’s URL, such as the my-mock in https://my-mock.mock.<team-domain>.postman.dev. Defaults to the mock’s name followed by six hexadecimal characters, such as orders-3f9c1a. A mock with no name gets new-mock- and six hexadecimal characters instead. Redeploying keeps the existing slug.

--public

Makes the mock server public. Mock servers are private by default.

--auto-deploy

Redeploys the mock server automatically whenever the cloud mock changes. Without this option, the mock server isn’t redeployed automatically.

-w, --workspace <id>

The ID of the workspace that contains the mock. Defaults to the workspace in your .postman/resources.yaml file.

-y, --yes

Accepts the defaults and skips all prompts. The mock server is private with auto-deploy off.

--api-key <key>

Your Postman API key. Use it to authenticate instead of signing in with postman login.

Example

postman mock deploy 12345678-90ab-cdef-1234-567890abcdef \
--name "My Mock" --slug my-mock --auto-deploy

postman mock log

You can use the postman mock log command to browse a deployed mock server’s request and response history, with the newest entries first. Provide the mock server’s ID, which you can find with the postman mock get command.

By default, the command streams the entries interactively. Use the --json option for machine-readable output.

The postman mock log command is available on Postman Solo, Team, and Enterprise plans. For more information, see the pricing page.

To use this command, sign in to Postman with the postman login command, or pass an --api-key.

Usage

postman mock log <mock-server-id> [options]
<mock-server-id>

The ID of the deployed mock server whose logs you want to view.

Options

--method <method>

Filters entries by HTTP method, such as GET or POST.

--status <code-or-range>

Filters entries by response status. Accepts an exact code, such as 404, or a class range, such as 5xx.

--path <pattern>

Filters entries by request path. Supports the * and ? wildcards.

--since <duration>

Shows only entries newer than the given duration, so --since 7d shows the last seven days. Use a whole number with a unit of ms, s, m, h, or d, such as 30m, 2h, or 7d.

--until <duration>

Shows only entries older than the given duration, so --until 2h hides the last two hours. Combine it with --since to bound both ends of a window. For example, --since 14d --until 7d shows the seven days that ended a week ago. Use a whole number with a unit of ms, s, m, h, or d, such as 30m, 2h, or 7d.

--limit <number>

The maximum number of entries to show.

--json

Outputs the logs in JSON format instead of streaming them interactively.

--api-key <key>

Your Postman API key. Use it to authenticate instead of signing in with postman login.

Example

postman mock log 12345678-90ab-cdef-1234-567890abcdef \
--status 5xx --since 7d --limit 50