This example demonstrate the HTTP-API using the cURL command-line utility.

First, create a directory with restricted access for storing the session cookie safely.

mkdir -m 0700 ~/.mmonit

Fetch the zsessionid cookie and store it in our cookie store

curl -c ~/.mmonit/cookie \
 http://127.0.0.1:8080/index.csp

Login with username and password. The zsessionid cookie is read from the file with -b, and -c writes the new cookie M/Monit returns when you log in. M/Monit versions before 4.4.0 should also add z_csrf_protection=off which turns CSRF protection off for this session so you don’t have to send a CSRF token with every request. From M/Monit version 4.4.0, a program needs no CSRF token and M/Monit ignores the z_csrf_protection parameter.

curl -b ~/.mmonit/cookie -c ~/.mmonit/cookie \
 -d z_username=admin \
 -d z_password=swordfish \
 -d z_csrf_protection=off \
 http://127.0.0.1:8080/z_security_check

After login, we can start querying for data. For instance, the host status list

curl -b ~/.mmonit/cookie \
 http://127.0.0.1:8080/api/2/status/hosts/list

Logout (invalidate the given zsessionid cookie).

curl -b ~/.mmonit/cookie \
 -X POST \
 http://127.0.0.1:8080/api/2/session/invalidate

Note:

For simplicity, the login example above shows password used on the command line, in practice it is more secure to store credentials in a protected file which is used with the -d option to curl.

curl -b ~/.mmonit/cookie -c ~/.mmonit/cookie \
 -d @/home/myusername/.mmonit/credentials \
 http://127.0.0.1:8080/z_security_check