Apidog

All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free
Home / Tutorials / Postman CLI: Advanced Automation for API Workflows

Postman CLI: Advanced Automation for API Workflows

Postman has long been a staple in the API testing space as we all know. In this article, we'll review the CLI and how to use it to speed our development process quickly.

We build APIs every day, and the need for automation, testing, and orchestration is paramount. Postman, a tool well-known for its user-friendly interface, introduced its Command Line Interface (CLI) to support deeper automation and CI/CD workflows.

Postman CLI is not just a tool for triggering tests; it's an expansive utility that allows you to script API interactions, integrate with existing pipelines, and ensure the integrity of APIs across multiple environments. This article delves into the advanced capabilities of the Postman CLI, showing how it can elevate your API development and testing practices.

The Evolution of Postman’s CLI

Before diving into the technical details, it’s important to understand why Postman introduced its CLI. Postman’s graphical interface is perfect for interactive use—ideal for exploring, testing, and debugging APIs.

However, in a modern DevOps environment, automation is crucial. Continuous Integration/Continuous Deployment (CI/CD) pipelines require automated testing and validation of APIs, and that's where Postman’s CLI comes in. It bridges the gap between manual API testing and fully automated, integrated API workflows. This utility enables teams to run collections, tests, and integrations directly from a terminal, unlocking the potential for efficient and repeatable processes in an automated environment.

How to Install and Setup Postman CLI?

Postman CLI isn't a standalone tool; it requires a configured environment, usually with a Postman API key to authenticate access to your workspace, collections, and environments. This is critical for the secure and seamless execution of scripts in environments, especially when integrating with CI/CD platforms.

We'll need to install the CLI to set it up. The Postman docs have a good documentation guide that can help.

Windows installation

Run the following commands to install the Postman CLI for Windows. This will download an install script and run it. The install script creates a %USERPROFILE%\AppData\Local\Microsoft\WindowsApps directory if it doesn't exist yet, then installs a postman binary there.

powershell.exe -NoProfile -InputFormat None -ExecutionPolicy AllSigned -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://dl-cli.pstmn.io/install/win64.ps1'))"

Mac (Apple silicon) installation

Run the following command to install the Postman CLI for Macs with an Apple silicon processor. This will download an install script and run it. The install script creates a /usr/local/bin directory if it doesn't exist yet, then installs a postman binary there.

curl -o- "https://dl-cli.pstmn.io/install/osx_arm64.sh" | sh

Mac (Intel) installation

Run the following command to install the Postman CLI for Macs with Intel chips. This will download an install script and run it. The install script creates a /usr/local/bin directory if it doesn't exist yet, then installs a postman binary there.

curl -o- "https://dl-cli.pstmn.io/install/osx_64.sh" | sh

Linux installation

Run the following command to install the Postman CLI for the latest Linux version. This will download an install script and run it. The install script creates a /usr/local/bin directory if it doesn't exist yet, then installs a postman binary there.

curl -o- "https://dl-cli.pstmn.io/install/linux64.sh" | sh

Once installed, authenticate using your Postman API key, which is securely managed by Postman’s backend:

postman login --with-api-key ABCD-1234-1234-1234-1234-1234

This API key management ensures that CLI executions have the same level of access and security as any session in the Postman web or desktop interface.

Running Collections and Environments

Postman CLI allows for execution of collections, the heart of Postman testing. Collections in Postman represent workflows or sequences of API calls that need to be tested. They are often tied to environments, which store variables like base URLs, authentication tokens, or any other configuration details that vary across deployment stages (e.g., dev, staging, production).

One of the more advanced features is the ability to run a collection with a specific environment. This allows for complex API flows to be tested in multiple environments without modifying the collection itself:

postman run <collection.json> --environment <environment.json>    

This simple yet powerful command permits multiple test suites to run across different configurations, making the CLI invaluable for orchestrating API tests in CI/CD pipelines.

Advanced Configuration: Global and Environment Variables

Postman CLI’s handling of variables is another advanced feature. With the introduction of dynamic environments, you can override and manage global, environment, and local variables on the fly. This capability is essential for advanced workflows where multiple API parameters need to change dynamically across different stages of testing.

Consider a scenario where your API requires dynamic authentication tokens. Rather than hardcoding tokens, you can inject these dynamically using Postman CLI:

postman run <collection.json> --env-var token=<new_token>               ‌

In more complex scenarios, you may want to load these values from external files, manage them via Postman’s collection pre-request scripts, or even pass them as environment variables during runtime from your CI platform. This flexibility makes the Postman CLI extremely potent for handling sophisticated API authentication flows.

Test Execution and Custom Reporting

Postman CLI supports the execution of Postman collections that are embedded with automated tests, written in JavaScript. These tests can check the integrity of API responses, validate schema, or compare response times against predefined thresholds.

When running collections, the CLI provides detailed logging and reporting. By default, the output is rendered in a structured format that shows which tests passed or failed. However, Postman CLI goes beyond basic logging. Advanced users can generate custom reports in multiple formats, including JSON, HTML, and JUnit XML, which is critical for integration with CI platforms like Jenkins or GitLab:

postman run <collection.json> --reporters cli,json --reporter-json-export output.json

This command will not only execute the collection but also output the results in both CLI and JSON formats, allowing for further manipulation or integration with dashboarding tools.

Deep Dive: Working with Newman vs. Postman CLI

Before the introduction of Postman CLI, Newman was the go-to tool for running Postman collections from the command line. While Newman remains relevant, especially for certain legacy workflows, Postman CLI brings several enhancements. It integrates directly with Postman’s cloud platform, offering more seamless authentication and environment management.

For example, Postman CLI can access shared workspaces directly from Postman’s cloud, eliminating the need to download and manage collections and environments locally. It also supports advanced authentication mechanisms (OAuth2, bearer tokens, etc.) natively through Postman’s workspace configurations, making it a more robust solution for cloud-integrated workflows.

Here’s a comparison of a simple collection run using Newman and Postman CLI:

Newman:

newman run <collection.json> --environment <environment.json>              

Postman CLI:

postman run <collection>

The main difference here is Postman CLI’s ability to reference collections and environments directly from the Postman workspace in the cloud, making it much more flexible for environments with high security or rapidly changing configurations.

Integrating Postman CLI into CI/CD Pipelines

One of the main reasons for using Postman CLI is its integration with CI/CD pipelines. In a pipeline, automated API testing becomes crucial for detecting issues early in the development cycle. Tools like Jenkins, CircleCI, GitLab CI, and GitHub Actions can all invoke Postman CLI commands to execute API tests at various stages of the pipeline.

Consider the following example for integrating Postman CLI into a Jenkins pipeline:

pipeline {
    agent any
    stages {
        stage('Run Postman Tests') {
            steps {
                script {
                    sh 'postman run <collection> --reporters cli,junit --reporter-junit-export results.xml'
                }
            }
        }
    }
    post {
        always {
            junit 'results.xml'
        }
    }
}

This Jenkinsfile runs the Postman collection and captures the test results in a format compatible with Jenkins' JUnit plugin, which then generates reports based on the test outcomes.

Postman CLI also allows for more granular control over test execution within pipelines. For example, you can define specific iterations of test suites, control timeouts, and configure retry mechanisms if a test fails, providing a highly customizable testing environment.

Handling Failures and Debugging

API tests can fail for numerous reasons, ranging from response format discrepancies to environment misconfigurations. Postman CLI supports detailed logging, which is essential for debugging such issues. Using the --verbose flag, developers can access a deep layer of information, including request and response bodies, headers, and execution timings.

postman run  --verbose

When integrated into CI/CD workflows, this level of granularity is invaluable. The verbose output can be piped into logs that developers can analyze to determine if the issue lies within the API itself, the test configuration, or the environment setup.

Exploring Apidog CLI: A Superior Command-Line Experience for Automated Testing

Image of Apidog

While Postman CLI is popular for automating API workflows, Apidog CLI is a compelling alternative that offers a robust and flexible approach to automated testing, built specifically to streamline API scenario execution from the command line. Apidog, a competitor to Postman, offers a seamless, command-driven API testing experience tailored for developers who need a fast, automated solution for testing complex workflows in CI/CD environments.

Why Apidog CLI?

Apidog’s CLI provides all the capabilities you need to automate API testing, with a particular focus on ease of use, advanced configuration options, and powerful integration capabilities. It is designed to run Apidog test scenarios from the terminal, making it an excellent choice for teams that want to embed API testing directly into their development and deployment workflows. The CLI integrates effortlessly with Apidog's powerful cloud features and is optimized for running test scenarios in any environment, ensuring rapid feedback on API integrity across multiple stages of your pipeline.

Setting Up the Apidog CLI

To start using Apidog CLI, ensure your environment is equipped with Node.js version 16 or higher. Apidog CLI can be easily installed via npm, allowing you to get up and running in just a few commands:

npm install -g apidog-cli

Once installed, verifying your setup is simple. The following command checks your Node.js version, CLI version, and the installation paths of Node, npm, and Apidog:

node -v && apidog -v && which node && which npm && which apidog

This ensures that your system is correctly configured for using Apidog CLI and helps troubleshoot any setup issues.

Running Test Scenarios in Apidog CLI

Apidog CLI is designed to run test scenarios efficiently, providing you with flexibility in how you authenticate and execute tests. You can specify a test scenario either by its unique ID or by referencing a folder that contains a collection of test scenarios. This feature simplifies the execution of large-scale API tests across multiple environments and configurations.

Here's an example of running a test scenario using an Apidog access token:

apidog run --access-token <your-access-token> -t <test-scenario-id>

This command executes the test scenario based on its ID, while securely passing authentication credentials via the access token. If you need to run all the test scenarios in a particular folder, you can simply use the folder ID instead:

apidog run --access-token <your-access-token> -f <test-scenario-folder-id>

Advanced CLI Options for Detailed Customization

One of Apidog CLI’s strengths lies in its advanced command-line options. You can configure test reports, environment variables, SSL certificates, and even introduce delays or timeouts between requests. Apidog allows fine-grained control over your testing process, whether you’re running a single test scenario or executing a batch of tests iteratively across different environments.

Here’s an example of generating a custom report in both JSON and JUnit formats, and saving it to a specific directory:

apidog run <collection> --reporters json,junit --out-dir ./test-reports

With Apidog CLI, you can also define environment variables dynamically, ensuring that your test scenarios are context-sensitive and able to adapt to changes in API configurations without modifying the core scripts:

apidog run <collection> --env-var "BASE_URL=https://staging.api.com" --env-var "TOKEN=abc123"

This kind of flexibility ensures that Apidog fits seamlessly into your existing testing infrastructure.

SSL Certificates and Security Testing

In today’s world, SSL validation is crucial when testing APIs. Apidog CLI supports passing client certificates, allowing secure connections to your APIs during tests. Whether you're using a single certificate or multiple certificates based on specific domains, Apidog simplifies the process. You can specify a single client certificate for your tests:

apidog run <collection> --ssl-client-cert ./path/to/cert.pem --ssl-client-key ./path/to/key.pem

Or, for more complex setups, you can use a JSON file that contains a list of client certificates matched to specific URLs:

apidog run <collection> --ssl-client-cert-list ./path/to/ssl-cert-list.json

Why Switch to Apidog?

Apidog CLI offers an enhanced experience for teams looking for faster performance, easier integration, and more control over their API testing workflows. It provides the flexibility of powerful command-line tools while integrating with Apidog’s feature-rich platform, making it a strong alternative to Postman’s CLI.

If you’re already familiar with Postman, transitioning to Apidog CLI will feel intuitive, but you’ll immediately notice the additional flexibility and power that Apidog offers. Its rich set of features, combined with extensive reporting and dynamic environment management, makes Apidog a strong contender for developers who need an efficient, scalable API testing solution.

button

Conclusion

In conclusion, while Postman has long been a staple in the API testing space, Apidog's CLI offers a robust alternative tailored for modern, automated workflows. With its advanced command options, seamless environment management, and flexible reporting capabilities, Apidog CLI empowers teams to efficiently test and validate APIs across various stages of development and deployment. If you’re looking for a powerful, flexible tool that integrates easily into your CI/CD pipelines, Apidog CLI is worth exploring. It offers a streamlined approach to API testing, ensuring that your APIs remain reliable, secure, and performant.

Join Apidog's Newsletter

Subscribe to stay updated and receive the latest viewpoints anytime.

Please enter a valid email
Network error, please try again later
Thank you for subscribing!