Run Postman collections using Newman and Python

Run Postman collections using Newman and Python


3 min read

In this article, we are going to run a Postman collection using the Newman and send the output of the Newman command through email.

Requirements :


Postman is a collaboration platform for API development. Postman's features simplify each step of building an API and streamline collaboration so you can create better APIs—faster(read more).

Download and Install Postman.


Newman is a powerful command-line collection runner for Postman. It allows you to run and test a Postman collection directly from the command-line. Newman maintains feature parity with Postman and allows you to run collections the same way they are executed inside the collection runner in Postman.

Installation :

Execute the below command to install Newman into your system.

$ npm install -g newman

Offline Mode

Export Postman Collection

Follow the below steps to export the collection from postman.
Select the collection -> Right click -> Select Export -> Save the file.


Run newman

Now we are ready to run the collection using newman.

$ newman run path/to/collection/collection_name.postman_collection.json


-e : is used to provide Postman Environment.

-g : is used to provide Postman Global Variables.


$ newman run path/to/collection/collection_name.postman_collection.json -e path/to/environment_file/environment_name.postman_environment.json

Online Mode

Get Postman API Key :

To access our collections using Postman API first we have to the get the API Key from Postman. You can generate your Postman API Key by visiting Postman API Keys page.


Click on "Generate API Key".


Give a name to your API key.


Copy the API Key to clipboard.

We need the id of the collection in order to access it through postman API. The collection_id can be retrieved from the Postman collections API.


Add the API Key which you copied earlier in the Postman request header.


Send the request. Now you can find the id of the collection in the API's response.


A collection can be accessed by using the Postman Collections API.

API URL sample:

Run your collection using Newman

$ newman run ''

The both commands (online and offline) will return the same output.


Now we came to our primary task, Which is running the newman and send the output / test report through email using Python. Let's do that!.

import sys
import subprocess
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

num_of_inputs = len(sys.argv)
collection = sys.argv[1]
environment = None
global_vars = None

if num_of_inputs > 2:
    environment = sys.argv[2]
if num_of_inputs > 3:
    golbals = sys.argv[3]

command = "newman run "+collection
if environment:
    command += " -e "+environment
if global_vars:
    command += " -g "+global_vars

command = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, error = command.communicate()

if error:
    email_body = "Error while running the command"
    email_body = output.decode('utf-8')

sender_email = "your email address"
password = "email password"
receivers = "receiver email address" # or [list of receivers]

email_message = MIMEMultipart("alternative")
email_message["Subject"] = "API Test Report"
email_message["From"] = "Sender name"
email_message["To"] = "receiver email address"

# to send as html
html_content = MIMEText(email_body, "html")

# to send as a plain text email
text_content = MIMEText(email_body, "text")

# using gmail smtp server
context = ssl.create_default_context()
with smtplib.SMTP_SSL("", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receivers, email_message.as_string())

Run the code. lets assume the programs name as, collection name as sample_collection.postman_collection.json and the environment file name as sample_environment.postman_environment.json.

#without environment
$ python sample_collection.postman_collection.json
#with environment
$ python sample_collection.postman_collection.json sample_environment.postman_environment.json

We made it!.


Schedule the execution of the program using a cron job. So that when ever the program is getting executed you will receive the test report via email.

Thanks for reading.

Please share your feedback and suggestion.

Did you find this article valuable?

Support Balasundar by becoming a sponsor. Any amount is appreciated!