Skip to main content

zapi-lib

Minimal Zabbix JSON-RPC API client for Python — a thin, httpx-only wrapper around the single /api_jsonrpc.php endpoint.

Spun out of zapi-mcp so that tools which only need the API client — e.g. speedtest-z — can depend on it without pulling in the MCP server stack (mcp, starlette, uvicorn, …).

Features

  • Version-adaptive auth: user + auth field (Zabbix ≤ 6.2) and username + Authorization: Bearer (6.4 / 7.0), degrading to the proven path automatically.
  • API tokens (Zabbix 5.4+): pass api_token= instead of a user and password; no user.login happens, and the token is checked once at construction.
  • Read helpers: get_hosts, get_items, get_problems, count_problems, get_events, get_maintenances.
  • Write helpers: set_host_tag (idempotent host-tag upsert that preserves other tags), acknowledge_problem, set_maintenance / set_maintenance_for_hosts (idempotent maintenance windows, by location tag or by exact host name).
  • Provisioning (ZapiProvisioner): config-driven client that auto-creates trapper hosts/items stamped with a managed-by tag — create_host, update_host, create_item, update_item, plus ensure_group / get_host_ids / get_item_ids.
  • Escape hatch: call(method, params) invokes any JSON-RPC method directly.
  • A single runtime dependency: httpx.

Install

pip install zapi-lib

Usage

from zapi_lib import ZapiClient, tag_filter

with ZapiClient("https://zabbix.example.com", "api-user", "api-pass") as z:
    hosts = z.get_hosts(tags=[tag_filter("speedtest-z")])
    z.set_host_tag("eduroam", "speedtest-z", "0.10.0")

The URL may be given with or without the /api_jsonrpc.php suffix.

To use a Zabbix API token instead of a password, pass it by keyword:

with ZapiClient("https://zabbix.example.com", api_token="<token>") as z:
    ...

The token is sent the same way as a session token (auth field before 6.4, Authorization: Bearer from 6.4) and checked once with a hostgroup.get of one row; a rejected token (or HTTP 401/403) raises ZapiAuthError. A user role that does not allow hostgroup.get is fine for ZapiClient: Zabbix only refuses the method after accepting the token. ZapiProvisioner with a group looks the group up with hostgroup.get, so its role must allow that method. Give either a token or a user and password, not both.

set_maintenance(location, since, till, name, description) opens an idempotent maintenance window over hosts carrying a matching location tag; set_maintenance_for_hosts(hosts, since, till, name, description) does the same over an explicit list of exact host (technical) names instead, for when the affected hosts don't share a tag or precise host-level control is wanted. Both are plain ZapiClient methods — no provisioning config required.

Both accept a keyword-only overwrite=False. The window name is name + the start time, so re-announcing the same planned outage with a corrected end time collides with the window already created for it. By default that collision is a no-op (the historical idempotent behaviour) and the correction is silently dropped. Pass overwrite=True to treat the collision as a correction and update active_since/active_till/timeperiods/ description in place instead. hostids is never sent on update, so an overwrite moves the schedule but never re-targets the window, and a repeat call with identical values still issues no write at all.

Note what overwrite does not cover: the start time is part of the window name, so a re-announcement that moves the start produces a different name, creates a second window, and leaves the earlier one in place. Two overlapping windows over-suppress rather than under-suppress, so it is the milder failure, but the stale window has to be removed by hand.

Only enable it where name + start time provably identifies one real-world event — a name generated from a site and its start time qualifies; a free-text name chosen per call does not, because two unrelated maintenances can then share a name and overwriting would silently reschedule someone else's window. get_maintenances() reads all maintenance windows back (hosts, host groups, time periods, tags) as raw API rows; deciding whether a given window is currently active is left to the caller.

Provisioning (config-driven)

ZapiProvisioner extends ZapiClient for metric-collection scripts that register the targets they push values to. It reads connection and provisioning defaults from a config.ini [zabbix] section and auto-creates trapper hosts/items tagged with a managed-by marker:

[zabbix]
url      = https://zabbix.example.com/api_jsonrpc.php
; or `user` / `password`
id       = api-user
pw       = api-pass
; or, instead of id/pw (leave them empty): an API token
; token  = <api-token>
; default host group for created/updated hosts
group    = DefaultGroup
; optional; added as a `location` tag
location = tokyo
; optional; managed-by marker tag on hosts/items
tag      = my-collector

Comments must be on their own lines: an inline ; ... after a value is read as part of the value (passwords may contain ;, so inline comments are not enabled).

from zapi_lib import ZapiProvisioner

with ZapiProvisioner.from_config() as z:  # ./config.ini, then ~/.config.ini
    z.show_version()
    host_ids = z.get_host_ids("pool-a") or z.create_host("pool-a", location="tokyo")
    host_id = host_ids[0]
    if not z.get_item_ids(host_id, "usage"):
        z.create_item(host_id, "usage", value_type=0)

update_host replaces the host's groups and tags (use set_host_tag to upsert a single tag instead).

License

MIT

Metadata

Release files for zapi-lib 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zapi-lib 0.9.0
File Size Uploaded
zapi_lib-0.9.0.tar.gz 32.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for zapi-lib 0.9.0
File Interpreter ABI Platform
zapi_lib-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 51.8 kB

Release files / zapi_lib-0.9.0.tar.gz

Download URL zapi_lib-0.9.0.tar.gz
Size 32.5 kB
Tags Source
SHA-256 checksum
How to use checksums
11e4f9aa3ed8b351b9543c443ba3773092a4f8e629513366dbf6c15c24c957cc
BLAKE2b-256 checksum
How to use checksums
7f3ffa869cc89323120cd0b3c21e80b5151d3a9e5611aea742e192d6e9db8c0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / zapi_lib-0.9.0-py3-none-any.whl

Download URL zapi_lib-0.9.0-py3-none-any.whl
Size 19.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c40c743f6edcc63d51f3199f536c6d96dc3e2fc09c59a8e133b5112473049ff
BLAKE2b-256 checksum
How to use checksums
cef7afd10274b40c48990f29c3c3babbb12b19aebb184048fa0e836a6122b455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page