Mock server commands
This topic covers mock server commands for the Postman CLI.
postman mock run
You can use the postman mock run command to start a mock, which is a code-based JavaScript API simulator, from a configuration file. The command isn’t supported for mocks deployed to the Postman cloud.
To run a collection against a mock, use the --mock option with the postman collection run command.
This command starts a mock from a configuration file in JSON format. With the configuration file, you can 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.
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.
Learn more at Build API mocks with JavaScript.
Usage
The path to the mock server configuration file in JSON format.
Options
Specifies the path to an environment file (JSON or YAML) loaded into pm.environment for the mock server’s scripts.
Specifies the path to a globals file (JSON or YAML) loaded into pm.globals for the mock server’s scripts.
The port the mock server 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.
Example
postman mock generate
You can use the postman mock generate command to generate a runnable mock from a Postman Collection or an OpenAPI 3.0 or 3.1 specification. The command auto-detects the source type. 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.
If you run the command without a source, it generates a sample mock with a GET /health endpoint that you can use as a starting point.
After you generate a mock, start it with the postman mock run command.
Learn more at Build API mocks with JavaScript.
Usage
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
The output directory for the generated mock. You can’t use this option with --update.
A display name for the generated mock.
The port the mock server listens on.
Overwrites the config.yaml or default.js file in the output directory if it already exists.
Updates an existing mock in place from the source. Provide the path to its config.yaml file, or the directory that contains it. This regenerates the default scenario handler and preserves the existing name, port, and scenarios. You can’t use this option with --output.
Always exits with code 0, even on failure.
Examples
postman mock get
Use the postman mock get command to show the details and run status of a mock.
Usage
The path to a mock, or a directory that contains one.
Options
Outputs the mock details in JSON format instead of a table.
Examples
postman mock list
Use the postman mock list command to list the mocks found under a directory.
Usage
The directory to search for mocks. If omitted, the command searches the current directory.
Options
Outputs the mock list in JSON format instead of a table.
Examples
postman mock delete
Use the postman mock delete command to permanently remove a mock artifact.
Usage
The path to the mock to delete, or a directory that contains one.
Options
Skips the confirmation prompt.