API Monitoring with Postman collection runner (Newman)

API Monitoring with Postman collection runner (Newman)

Postman is a great tool for API development, but I also use it to do part of my monitoring. The guys from Postman offer a monitoring service, but it's quite pricy if you have a lot of API endpoints and want to run it frequently.

With newman you can run your Postman collections on the command line. With a little configuration around it and a cronjob you can build your own API monitoring within minutes.

We want to not just run the collection, but also get an email notification when something fails. There are different kinds of reports we can generate, I prefer the html version of it. With it we can even put in a custom template and Marcos Ellys provides a great one.

Monitoring in 4 easy steps:

1. Install the necessary node modules:

npm install -g newman newman-reporter-html
  1. Pull the htmlreqres.hbs template from the template repository.
  1. Create a shell script (monitoring.sh) which combines the execution and the email dispatch in case of an error:
newman run --reporters html \
    --reporter-html-template path/to/htmlreqres.hbs \
    --reporter-html-export path/to/report.html \
    path/to/postman-collection.json \
    -e path/to/postman-environment.json
if [ $? -ne 0 ]; then
    echo "Error in monitoring (see attachment)" | mutt -a "/path/to/report.html" -s "Error in monitoring" -- youremail@address.com
fi

4. Create a cronjob to run the script every 10 minutes:

*/10 * * * * /path/to/monitoring.sh

That's it! Enjoy the low budget monitoring.