Skip to main content

OPNsense Python API bindings

Project description

opnsense-api

Typed Python bindings for the OPNsense REST API with Pydantic model validation.

Installation

pip install opnsense-api2

With Authentik SSO support:

pip install opnsense-api2[authentik]

Quick start

from opnsense_api.client import Client

client = Client(
    api_key="your-api-key",
    api_secret="your-api-secret",
    base_url="https://opnsense.local/api",
    verify_cert="/path/to/root-ca.pem",
)

# List all firewall aliases
aliases = client.firewall_alias_search()

# Add a DNS host override
client.unbound_add_host_override(hostname="server1", domain="lan", server="192.168.1.100")

# Get firmware status
info = client.firmware_info()

Desired state management

Declare your desired OPNsense configuration in Python or YAML and let the state manager figure out the diff:

state = client.state()

state.dns_host(hostname="server1", domain="lan", ip="192.168.1.100")
state.firewall_alias(name="trusted_hosts", type="host", content=["10.0.0.1", "10.0.0.2"])
state.firewall_rule(interface="lan", source_net="trusted_hosts", destination_port="443", action="pass")

changes = state.plan()   # preview changes
state.apply()            # apply after confirmation

Or from a YAML file:

state = client.state()
state.load_yaml("desired_state.yaml")
state.apply(auto_approve=True)

Supported APIs

Area Operations
Firewall Filter rules, aliases, categories, DNAT (port forward), SNAT, 1:1 NAT
HAProxy Servers, backends, frontends, ACLs, actions, settings, export, service control
Unbound DNS Host overrides, domain overrides, settings
Kea DHCPv4 Subnets, reservations, settings
Trust CAs, certificates, CRLs
ACME Client Accounts, certificates, validations
Interfaces Overview, VLANs
Auth Users, groups
Core Config backup/export, firmware info
Authentik SSO HAProxy + Authentik integration

CLI

The opn command provides quick access to common operations:

# Interactive config setup
opn config

# Create an HAProxy reverse proxy rule
opn haproxy --domain example.com --ip 10.0.0.5 --port 8080 \
    --frontend MAIN-HTTPS --ssl-provider "Letsencrypt"

# With Authentik SSO
opn haproxy --domain app.example.com --ip 10.0.0.5 --port 8080 \
    --frontend MAIN-HTTPS --ssl-provider "Letsencrypt" --sso authentik

# Toggle SSO on a domain
opn sso --domain app.example.com --enable
opn sso --domain app.example.com --disable

Configuration is resolved in order: config.yml < environment variables < CLI flags.

Development

pip install -r requirements.txt
python3 -m unittest test_suite.py

Inspired by pyopnsense.

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_api2-0.7.3.tar.gz (186.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_api2-0.7.3-py3-none-any.whl (120.2 kB view details)

Uploaded Python 3

File details

Details for the file opnsense_api2-0.7.3.tar.gz.

File metadata

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

File hashes

Hashes for opnsense_api2-0.7.3.tar.gz
Algorithm Hash digest
SHA256 5e9ddb151ba4da4cfa0ee29569abbfd942725c13eb8b6bd31d3aecfbdf48f3a3
MD5 7077e407ddda6cd0904db5d1b1cc27c3
BLAKE2b-256 e48be0b45e2270698fab3b7a4e7e0649a2fde203c52fd49b3b7c5d9127923642

See more details on using hashes here.

Provenance

The following attestation bundles were made for opnsense_api2-0.7.3.tar.gz:

Publisher: publish.yml on Park0/opnsense_api

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_api2-0.7.3-py3-none-any.whl.

File metadata

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

File hashes

Hashes for opnsense_api2-0.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4cf2ba3cbef2809c085a90525e507452c73505e0dcc4f21f8f852672ca92a7cb
MD5 0a6f74ae981c89a028acbf1a32a37447
BLAKE2b-256 77032e0e2f17a2d17889fcb060e6cf8f4400efecb87014e8e3830d8d37c04a19

See more details on using hashes here.

Provenance

The following attestation bundles were made for opnsense_api2-0.7.3-py3-none-any.whl:

Publisher: publish.yml on Park0/opnsense_api

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