Skip to main content

Python wrapper for the OPNsense REST API

Project description

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:

  1. OPNsenseClient — top-level entry point; manages an httpx.Client with Basic Auth. All API modules are lazy-loaded as @cached_property attributes.
  2. BaseModule — base class providing generic CRUD helpers (_search, _get_item, _add_item, etc.) and service control helpers.
  3. Module classes (modules/core/, modules/plugins/) — one per OPNsense subsystem, returning typed Pydantic models.
  4. Pydantic models (models/) — typed representations of OPNsense entities; all extend OPNsenseModel with extra="allow" so unrecognised fields are preserved.
  5. ExceptionsOPNsenseErrorOPNsenseHTTPErrorOPNsenseAuthError / OPNsenseNotFoundError; plus OPNsenseValidationError for 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opnsense_py-0.1.2.tar.gz (65.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opnsense_py-0.1.2-py3-none-any.whl (90.7 kB view details)

Uploaded Python 3

File details

Details for the file opnsense_py-0.1.2.tar.gz.

File metadata

  • Download URL: opnsense_py-0.1.2.tar.gz
  • Upload date:
  • Size: 65.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for opnsense_py-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c148abae4fdbe481e799d68defcad1c14214b3a3aff6f3ee4a8f281a53bcc8b0
MD5 419fd84893693677764154672ee5f9ed
BLAKE2b-256 278a6a9d97eeaaf72f3a71e372f27822ce3c95635c8ae039ce291f489c10bb96

See more details on using hashes here.

Provenance

The following attestation bundles were made for opnsense_py-0.1.2.tar.gz:

Publisher: workflow.yaml on singularcurio/opnsense-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file opnsense_py-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: opnsense_py-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 90.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for opnsense_py-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8ebdf414991d634c528f7b99f83381b3099ef7468d3758b1f76d09f6be1a6952
MD5 c6920b5b81c68c6d515f6eb5f0af028c
BLAKE2b-256 78e9c41282cde84da33ab2642b4a76329d15fddeeddd9bba131819f0cff6de05

See more details on using hashes here.

Provenance

The following attestation bundles were made for opnsense_py-0.1.2-py3-none-any.whl:

Publisher: workflow.yaml on singularcurio/opnsense-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page