A client for the unofficial pfSense API at https://github.com/jaredhendrickson13/pfsense-api
Project description
Pyfsense Client
Pyfsense Client is a Python API client for interacting with pfSense REST API endpoints provided by the package at https://github.com/jaredhendrickson13/pfsense-api.
This project now provides two versions of the API client:
- V2 API (Current): Developed entirely from scratch, this is the actively maintained version. It supports modern authentication methods (API key and JWT-based) and targets the latest pfSense API implementations.
- V1 API (Legacy Support): Full support for the original V1 endpoints remains available for backward compatibility, though new development is focused on V2.
The V1 code is currently being tested against pfSense 24.03 and the v1.7.6 API endpoints. The V2 code is currently being tested against pfSense 24.11 and the v2.3.3 API endpoints.
Table of Contents
- Installation
- Using the V2 API (Current)
- Using the V1 API (Legacy Support)
- Configuring Authentication
- Ignoring Certificate Validation
- Development
Installation
Install via pip:
pip install pyfsense-client
Using the V2 API (Current)
The V2 API is the recommended and actively maintained version of the Pyfsense Client. It supports both API key and JWT-based authentication.
Example Usage
from pyfsense_client.v2 import PfSenseV2Client, ClientConfig
# Configure your connection
config_data = {
"host": "example.com", # Base URL or IP of your pfSense instance
"verify_ssl": False, # Disable SSL verification if needed
"timeout": 30, # Request timeout in seconds
"username": "your_username", # For JWT-based auth (optional if using API key)
"password": "your_password", # For JWT-based auth (optional if using API key)
# "api_key": "your_api_key", # Alternatively, use API key based authentication
}
config = ClientConfig(**config_data)
client = PfSenseV2Client(config=config)
# For JWT-based authentication, you can call:
jwt_token = client.authenticate_jwt()
print("JWT token:", jwt_token)
# Example: Retrieve all firewall aliases
aliases = client.get_firewall_aliases()
for alias in aliases:
print(alias)
For additional endpoints (such as DHCP leases, applying firewall changes, etc.), refer to the V2 API documentation in the project docs.
Using the V1 API (Legacy Support)
If you need to interact with legacy systems or prefer the older endpoints, the V1 API client is still fully supported.
Example Usage
from pyfsense_client.v1 import PfSenseV1Client, ClientConfig
# Configure your connection for V1
config_data = {
"hostname": "example.com",
"mode": "api_token", # Options include "local", "jwt", or "api_token"
"client_id": "your_client_id", # Required for API token mode
"client_token": "your_client_token", # Required for API token mode
"verify_ssl": False,
}
config = ClientConfig(**config_data)
client = PfSenseV1Client(config=config)
# Example: Execute a shell command on the firewall
response = client.execute_shell_command("ls -la")
print(response)
Keep in mind that while the V1 API is still available, new features and improvements will be added only to the V2 implementation.
Configuring Authentication
V2 API
- API Key Authentication: Pass your API key in the configuration via the
api_keyfield. - JWT-based Authentication: Provide
usernameandpassword(or callauthenticate_jwt()to obtain a token). The token will be automatically attached to subsequent requests.
V1 API
The V1 client supports multiple authentication modes:
- Local: Requires
usernameandpassword. - JWT: Requires a valid
jwttoken. - API Token: Requires both
client_idandclient_token.
Ignoring Certificate Validation
If your pfSense instance uses self-signed certificates or you wish to disable SSL certificate validation, set verify_ssl=False in your client configuration.
Development
You can build a Docker image for development. This image will install all dependencies and mount the source code for live development.
Building the Docker Image
docker compose -f local.yml build
Running Unit Tests
docker compose -f local.yml up
Notes
The V1 implementation is a heavy rewrite of (https://github.com/yaleman/pfsense-api-client).
This README should help you get started with the new V2 API while still providing guidance for legacy V1 usage. Happy coding!
Project details
Release history Release notifications | RSS feed
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 pyfsense_client-0.4.3.tar.gz.
File metadata
- Download URL: pyfsense_client-0.4.3.tar.gz
- Upload date:
- Size: 40.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9fea330651969cd4153a69cd5cc660520a8c7ce846812249521d85c622ce731
|
|
| MD5 |
64420d74450229f43474265c0165d95e
|
|
| BLAKE2b-256 |
846d75f106f7968d15b18a5c27ac364bd827db4d7d3758f33a9e4e920e07a7df
|
File details
Details for the file pyfsense_client-0.4.3-py3-none-any.whl.
File metadata
- Download URL: pyfsense_client-0.4.3-py3-none-any.whl
- Upload date:
- Size: 47.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d372e9fb2d2ceab455b4861fb8215481c53236e84caf8e497236df629fbd0e70
|
|
| MD5 |
3c9654e94acb14f3a9bbf81fe1dcee51
|
|
| BLAKE2b-256 |
295e9df90af45faa7d2c2822387fe46b1450e941c4b081c13de4466eb0035356
|