opnsense-py
A synchronous Python client library and CLI for the OPNsense REST API.
Installation
# Library only
pip install opnsense-py
# Library + CLI (opn / opnsense commands)
pip install opnsense-py[cli]
Requires Python 3.12+.
CLI quick start
# HTTPS with self-signed certificate (most common)
opn --host 192.168.1.1 --no-verify-ssl --api-key KEY --api-secret SECRET system status
# Plain HTTP
opn --host 192.168.1.1 --no-tls --api-key KEY --api-secret SECRET system status
Configuration file
Store connection settings in ~/.config/opnsense-py/config.toml to avoid repeating flags:
[default]
host = "192.168.1.1"
api_key = "your-key"
api_secret = "your-secret"
verify_ssl = "false"
[prod]
host = "firewall.example.com"
api_key = "prod-key"
api_secret = "prod-secret"
Switch profiles with --profile prod or OPNSENSE_PROFILE=prod.
Environment variables
| Variable | Description |
|---|---|
OPNSENSE_HOST |
Hostname or IP |
OPNSENSE_API_KEY |
API key |
OPNSENSE_API_SECRET |
API secret |
OPNSENSE_VERIFY_SSL |
Set to false to skip TLS verification |
OPNSENSE_NO_TLS |
Set to 1 to use plain HTTP |
OPNSENSE_PROFILE |
Config file profile (default: default) |
Output formats
opn -o table firewall alias list # default: human-readable table
opn -o json firewall alias list # JSON (pipe-friendly)
opn -o plain firewall alias list # one value/UUID per line
Available commands
| Command | Description |
|---|---|
auth |
Users, groups, API keys |
captiveportal |
Captive portal zones, sessions, vouchers |
cron |
Cron jobs |
dhcrelay |
DHCP relay |
diagnostics |
ARP, routes, interfaces, firewall states, traffic |
dnsmasq |
Dnsmasq DNS/DHCP host entries and domain overrides |
firewall |
Aliases, rules, NAT (DNAT/SNAT) |
firmware |
Firmware updates and package management |
haproxy |
HAProxy frontends, backends, servers |
hostdiscovery |
Network host scanning |
ids |
Suricata IDS/IPS policies, rules, alerts |
ipsec |
IPsec VPN connections, child SAs, sessions |
kea |
Kea DHCP subnets and reservations |
monit |
Monit service checks, tests, alerts |
ntpd |
NTP daemon status |
openvpn |
OpenVPN instances, overwrites, sessions |
radvd |
Router Advertisement daemon |
routes |
Static routes |
routing |
Gateway definitions |
syslog |
Remote syslog destinations |
system |
System status, services, tunables, backups, reboot |
trafficshaper |
Traffic shaper pipes, queues, rules |
trust |
Certificate authorities, certificates, CRLs |
unbound |
Unbound DNS resolver hosts, forwards, ACLs |
wireguard |
WireGuard VPN servers and peers |
Run any command with --help for full usage.
Library usage
from opnsense_py import OPNsenseClient
with OPNsenseClient(
host="192.168.1.1",
api_key="your-key",
api_secret="your-secret",
verify_ssl=False,
) as client:
# List firewall aliases
aliases = client.firewall.search_aliases()
for alias in aliases.rows:
print(alias.name, alias.type)
# Add a cron job
from opnsense_py.models.cron import CronJob
result = client.cron.add_job(CronJob(
command="firmware",
description="Nightly firmware check",
minutes="0", hours="2",
days="*", months="*", weekdays="*",
))
print(result.uuid)
Architecture
The client is structured in layers:
OPNsenseClient— top-level entry point; manages anhttpx.Clientwith Basic Auth. All API modules are lazy-loaded as@cached_propertyattributes.BaseModule— base class providing generic CRUD helpers (_search,_get_item,_add_item, etc.) and service control helpers.- Module classes (
modules/core/,modules/plugins/) — one per OPNsense subsystem, returning typed Pydantic models. - Pydantic models (
models/) — typed representations of OPNsense entities; all extendOPNsenseModelwithextra="allow"so unrecognised fields are preserved. - Exceptions —
OPNsenseError→OPNsenseHTTPError→OPNsenseAuthError/OPNsenseNotFoundError; plusOPNsenseValidationErrorfor HTTP 200 responses with validation errors.
Development
# Install with dev dependencies
poetry install --extras cli
# Run tests
poetry run pytest
# Lint
poetry run ruff check .
# Type check
poetry run mypy opnsense_py/
# Run a single test
poetry run pytest tests/unit/modules/core/test_cron.py::test_search_jobs
Integration tests require a live OPNsense instance and are skipped by default:
OPNSENSE_HOST=192.168.1.1 OPNSENSE_API_KEY=... OPNSENSE_API_SECRET=... \
poetry run pytest -m integration
Metadata
Release files for opnsense-py 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| opnsense_py-0.1.3.tar.gz | 66.4 kB | With provenance | Details for opnsense_py-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| opnsense_py-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for opnsense_py-0.1.3-py3-none-any.whl |
Total release size: 157.7 kB
Release files / opnsense_py-0.1.3.tar.gz
| Download URL | opnsense_py-0.1.3.tar.gz |
|---|---|
| Size | 66.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c2a6e2eb86ad58fcfb5b204779f6a1df1308fee32f25e3486f06bd60ea3b98c7
|
|
BLAKE2b-256 checksum How to use checksums |
855366be50964478466537a22a0777a5e4f271b3f9966a35f30082fc14c17a20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Mar 28, 2026.
Transparency logRelease files / opnsense_py-0.1.3-py3-none-any.whl
| Download URL | opnsense_py-0.1.3-py3-none-any.whl |
|---|---|
| Size | 91.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8a48d92535020f588b831934e0f21445ee14c86655ca85a502ec0354bc88465a
|
|
BLAKE2b-256 checksum How to use checksums |
0e6bc6574a4f7d3eb1227a04471a180e181d0757d0d91ddb6b22dabe80aa67b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Mar 28, 2026.
Transparency logSecurity for opnsense-py 0.1.3
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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for opnsense_py-0.1.3.tar.gz
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 Mar 28, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.