Getting started with the Zabbix API – Java example

Zabbix provides an API which can be used to automate routine task, integrate Zabbix functionality into your internal apps or you may even write your own client application.

The API is based on JSON-RPC so you need to use the JSON encoded commands to communicate with the API.

In the following example we will authenticate with the Zabbix server (get the authentication token), which is mandatory before making any other API calls.

All other API calls are done in a similar way, so I guess this example is enough to get started.

The example uses DavidWebb HTTP-Client (with dependecies) which makes JSON REST calls very simple.

import org.json.JSONObject;
import org.json.JSONException;
import com.goebl.david.Webb;

public class ZabbixAPIExample {

   public static void main(String[] args) {

     try {

       JSONObject mainJObj = new JSONObject();
       JSONObject paramJObj = new JSONObject();

       mainJObj.put("jsonrpc", "2.0");
       mainJObj.put("method", "user.login");

       paramJObj.put("user", "Zabbix_username");
       paramJObj.put("password", "Zabbix_user_password");

       mainJObj.put("params", paramJObj);
       mainJObj.put("id", "1");

       Webb webb = Webb.create();

       System.out.println("Data to send: " + mainJObj.toString());

       JSONObject result ="http://zabbix_server_url/api_jsonrpc.php")
                                    .header("Content-Type", "application/json")

       System.out.println("Authentication token: " + result.getString("result"));

     } catch (JSONException je) {

       System.out.println("Error creating JSON request to Zabbix API..." + je.getMessage());




One thought on “Getting started with the Zabbix API – Java example”

  1. Hey.

    Using your example for a while,authorization went fine,but i’m stuck with getting host inventory(coordinates).
    My params look like that :

    mainJObj.put(“jsonrpc”, “2.0”);
    mainJObj.put(“method”, “host.get”);
    mainJObj.put(“output”, “extend”);

    paramJObj.put(“selectInventory”, “location_lat”);
    paramJObj.put(“selectInventory”, “location_lon”);
    paramJObj.put(“Output”, “extend”);
    paramJObj.put(“output”, “location_lat”);

    mainJObj.put(“params”, paramJObj);
    mainJObj.put(“id”, “2”);
    mainJObj.put(“auth”, “my_token”);

    and the response i’m getting :

    Host info: [{“hostid”:”10151″,”inventory”:{“hostid”:”10151″}},{“hostid”:”10152″,”inventory”:{“hostid”:”10152″}}

    It looks like it’s showing right things,but not showing exactly what i need(i need to get host coordinates),any ideas how i can fix my problem?


Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image