Unboxing of Postman CLI
Tags: postman, postmancli, devops, github
Published: 2022-10-09 15:05:22

## Introduction
We all use Postman to manually test APIs, but if we want to automate API testing using the command line, we should try `Postman CLI`. With the launch of `Postman v10`, the product team has simplified the lives of users by automatically generating the CLI commands that enable us to test & run the collections in any CI/CD pipelines. The good news is that all test results will instantly sync to Postman, allowing users to conduct additional testing failure analysis and troubleshooting using Postman Collection Runs.

## Installation of Postman CLI

- Run the below command in the `terminal` based on your Operating System

  > Windows Installation
  
``` bash
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 (Intel) Installation
 
``` bash
curl -o- "https://dl-cli.pstmn.io/install/osx_64.sh" | sh
```
 
  > Linux Installation
``` bash
curl -o- "https://dl-cli.pstmn.io/install/linux64.sh" | sh
```
  > Let's check the version `postman -v`
  
![CLI Command version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6eniu7d49s1ixxrdbp4.png)

## Run collections using terminal

We can now run the collections locally as Postman CLI has been installed

> You need the API key to log in on Postman CLI and get authorization for running your collections

**Step 1 :** `Generate your Postman API Key` or you can use existing one as below


![API Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhzea16jbdzt2rh4ck5f.png)


- **Generate key**

![Generate API Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhbpdmts27qz8gxyakjg.png)

- **Use Existing key**

![Use Existing Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6majutg25a2r2h9hupn.png)

**Step 2 :** Copy the auto generated command from Postman CLI and paste it to your terminal and hit `Enter`

> The process will first authenticate you using your API Key, run the collections, and log you out of the terminal

![CLI Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4b7j0x3sagfq8lk7gda.png)


![CLI run](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3gfotbb9rjku1hbqoj2.png)


**Step 3 :** Users can further investigate test failures on the Postman using the run data link generated on the terminal.


![Results Check](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxt7mm5t5r1ulb6zdq4v.gif)

**Step 4 :** `postman logout`

![CLI Logout](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdgpmcfo4zmbc4phc195.png)

💡**Bonus Tips**

📌 How do you run a collection N times?

> Pass the Iteration count using the `-n` option, as shown below.


``` bash
postman login --with-api-key <<Provide your API Key>>
postman collection run <<Collection ID>> - n <<Iteration Count>>
```

![Iterations Runs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsaqogbug4xrlcbi0q84.gif)


📌 If we check and uncheck the collection folders or `rearrange` the Requests in Runner, how can we auto generate the CLI script?

> Let's see how Postman does this magic for us

![Rearrange](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9zh4wq08k8td2g4f1ty.gif)

## Run collections using GitHub Actions

![Postman & Github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wc1k2e1ozpz4gllr0u1w.png)

Once the commands have been tested locally, we are ready to use them in our CI/CD pipelines. This enables us to run our tests and validations on CI/CD using Postman CLI configuration and seamlessly link the results back to Postman.

**Step 1 :** Click `Configure Command` under **Run on CI/CD** section

![CICD Configure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uy37x5hne888dqtgkgss.png)

**Step 2 :** Select configurations such as Collection, Environment, CI/CD Provider `OS for CI/CD` will get auto selected with respect to Provider. `Copy` the auto generated script.


![CLI Configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbh2lztmxuip05j2e6pn.gif)

**Step 3 :** **Paste** the script into GitHub Actions pipeline. `Ex: dev-pipeline.yml` file

> Feel free to experiment with the yaml script by adding Events, Jobs, Actions and Runners to the workflow

![Gitpipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51eq24osno7ce6m2x7q8.png)

**Step 4 :** Save the Postman `API Key` as a secret environment variable in GitHub repo to prevent exposure.

![Secret](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eroxsmbfgj5udmhxxtkf.png)

**Step 5 :** **Run** this workflow manually from the Actions tab

![workflow1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sr54udo6kof7n5op1bnj.png)

![wf2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/llgt48g0e9sej77468i0.png)

**Step 6 :** Check the results using the `run data link` displayed in the pipeline logs

![wfresults](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ak46smwjgoko6h95kxgl.png)

> This workflow will be triggered automatically for every `push` or `pull` request event made for the "**main**" branch

## Collection Runs

We can view the information of previous collection runs done via Collection Runner or Postman CLI by selecting the **Runs** tab under the collections. You can share the report with your team member's using the **share** option if you want them to review the failed tests, if they are already part of the workspace they can access the reports directly without the invite.

![Reports](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n27si8a4eed6ognuwlhj.gif)

## Conclusion

Hopefully you found it interesting to learn how to use Postman CLI for running the collections locally, using CI/CD pipelines & checking the reports via Postman Runs.