A Python client library for OPNsense API.
Project description
OPN API
A Python client for the OPNsense REST API.
This library is based on opn-cli (v1.7.0) by Andreas Stürz, with additional code from python-opnsense by Dylan Turnbull. It has been stripped down to focus solely on API implementation while maintaining an extensible structure for easy addition of new API functions.
Tested against OPNsense versions 24 and 25. Fully supports OPNsense 25.7+ API format changes.
Features
- Supports OPNsense API calls.
- Built for Python 3.12+ (but likely compatible with older versions).
- Designed for easy extension and customization.
- Includes support for core OPNsense functionalities like firewall, routing, VPN, syslog, and plugins.
Installation
You can install the OPN API client using pip:
pip install opn-api
Alternatively, if you want to install from source:
git clone https://github.com/devinbarry/opn-api.git
cd opn-api
pip install .
Usage
Initializing the Client
from opn_api.api.client import OPNAPIClient, OPNsenseClientConfig
# Configure API client
config = OPNsenseClientConfig(
api_key="your_api_key",
api_secret="your_api_secret",
base_url="https://your-opnsense-instance/api",
ssl_verify_cert=False # Set to True if using valid SSL certs
)
# Create API client instance
client = OPNAPIClient(config)
Example: Firewall Alias Management
from opn_api.client import OPNFirewallClient
from opn_api.models.firewall_alias import FirewallAliasCreate
fw = OPNFirewallClient(client)
# Retrieve a list of all aliases
aliases = fw.alias.list()
for alias in aliases:
print(alias)
new_alias = fw.alias.add(FirewallAliasCreate(
name="MyAlias",
type="host",
content=["192.168.1.100"],
description="My test alias"
))
print(new_alias)
Example: Firewall Rules Management
from opn_api.client import OPNFirewallClient
from opn_api.models.firewall_models import FirewallFilterRule
firewall = OPNFirewallClient(client)
# Get all rules
rules = firewall.filter.list_rules()
print(rules)
# Retrieve firewall rule by UUID
rule = firewall.filter.get_rule("your-rule-uuid")
print(rule)
# Add a new firewall rule
new_rule = firewall.filter.add_rule(FirewallFilterRule(
sequence=1,
action="pass",
protocol="TCP",
source_net="192.168.1.0/24",
destination_net="8.8.8.8/32",
description="Allow Google DNS"
))
print(new_rule)
Example: Fetching DHCP Leases
from opn_api.client import OPNFirewallClient
fw = OPNFirewallClient(client)
# Fetch DHCP leases
leases = fw.dhcp.list_leases()
print(leases)
Example: Download Configuration Backup
from opn_api.api.core.configbackup import Backup
backup = Backup(client)
config_data = backup.download()
with open("opnsense_backup.xml", "wb") as file:
file.write(config_data.encode("utf-8"))
Example: Fetching Firmware Information
from opn_api.api.core.firmware import Firmware
firmware = Firmware(client)
info = firmware.info()
print(info)
Running Tests
To run the test suite, ensure pytest is installed:
pip install pytest
Then execute:
pytest tests/
Contributing
Contributions are welcome! Please open an issue or a pull request if you would like to add features or fix bugs.
License
This project is licensed under the AGPLv3 License. See the LICENSE file for details.
For further details and documentation, visit OPNsense API Reference.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opn_api-0.5.0.tar.gz.
File metadata
- Download URL: opn_api-0.5.0.tar.gz
- Upload date:
- Size: 40.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8eb36156ecbd52a2d026a71379c2a6ca9d05e89192d1c2557569ff8207504b79
|
|
| MD5 |
d89dc9931b9309a85c2e994fc9e3cdd0
|
|
| BLAKE2b-256 |
28136eacd303c7933b6d44a3b8d1c3c5d8cf71d87ccbe9595130a6bdd2e639a5
|
File details
Details for the file opn_api-0.5.0-py3-none-any.whl.
File metadata
- Download URL: opn_api-0.5.0-py3-none-any.whl
- Upload date:
- Size: 47.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14caff0d03814c79de261ebd8a41283fd576e4c8da3cbd0a3b45645e765709d8
|
|
| MD5 |
94ff134e5655e298574d1b1e26eb0a70
|
|
| BLAKE2b-256 |
cad4de38f202cc1884caac67f70caa9df9e81ca8ad9c8a56d2b914e066b363e6
|