aiofortiosapi
Async Python client for the FortiOS REST API, built for the Home Assistant fortios integration.
Community project — not affiliated with or supported by Fortinet TAC.
Install
pip install aiofortiosapi
Usage
The library requires an injected aiohttp.ClientSession — Home Assistant provides one via
async_get_clientsession(hass). You own the session lifecycle; this library never creates or closes it.
import asyncio
import aiohttp
from aiofortiosapi import FortiOSClient, FortiOSAuthenticationError, FortiOSConnectionError
async def main() -> None:
session = aiohttp.ClientSession()
try:
client = FortiOSClient(
host="192.168.1.1",
token="your-rest-api-token",
session=session,
verify_ssl=False, # set True in production with a valid cert
)
status = await client.get_system_status()
print(status.hostname, status.version)
usage = await client.get_resource_usage()
print(f"CPU {usage.cpu_percent}% MEM {usage.memory_percent}%")
devices = await client.get_detected_devices()
for d in devices:
print(d.mac, d.hostname, d.ip, "online" if d.is_online else "offline")
except FortiOSAuthenticationError:
print("Bad token — re-enter credentials")
except FortiOSConnectionError:
print("Cannot reach the FortiGate — check host/port")
finally:
await session.close()
asyncio.run(main())
Generating a FortiOS REST API token
- In the FortiGate GUI go to System → Administrators → Create New → REST API Admin.
- Set a Trusted Host (the IP of your Home Assistant instance) to restrict token use.
- Assign a read-only profile (
prof_adminor custom). - Copy the generated token — it is shown only once.
The library uses Authorization: Bearer <token> (not the legacy ?access_token= query string).
Device online state
DetectedDevice.is_online uses the flag reported by FortiOS. On firmware that
omits the field, it falls back to deriving online state from last_seen
freshness: a device counts as online when seen within
DEFAULT_ONLINE_THRESHOLD seconds (300). Tune the fallback per client:
client = FortiOSClient(..., device_online_threshold=600)
Scope
aiofortiosapi is intentionally minimal:
- Three typed monitor endpoints (
get_system_status,get_resource_usage,get_detected_devices) used by the Home Assistant integration. - A generic
get(path)for any other endpoint that returns the raw JSON envelope. - No config-write, CMDB, file upload, SSH fallback, or CLI helpers.
- No session/cookie login flow — Bearer token only.
This keeps the dependency tree small (runtime dep: aiohttp only) and passes HA integration
quality review requirements.
Exception hierarchy
| Exception | When raised | HA mapping |
|---|---|---|
FortiOSConnectionError |
Transport/timeout failure | ConfigEntryNotReady |
FortiOSAuthenticationError |
HTTP 401 / 403 | ConfigEntryAuthFailed |
FortiOSNotFoundError |
HTTP 404 | log / inspect |
FortiOSResponseError |
Bad JSON, 5xx, other 4xx | log / raise |
License
MIT
Release files for aiofortiosapi 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aiofortiosapi-0.1.0.tar.gz | 14.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aiofortiosapi-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.1 kB
Release files / aiofortiosapi-0.1.0.tar.gz
| Download URL | aiofortiosapi-0.1.0.tar.gz |
|---|---|
| Size | 14.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a7045770890c87190921bc1964bf5f24d6f500fa906b899c55fdd1f474ba1581
|
|
BLAKE2b-256 checksum How to use checksums |
c6b6dad0b5c5c70e300864019326463b03c84ab0c4be0b401d71efa1a560a499
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 26, 2026.
Transparency logRelease files / aiofortiosapi-0.1.0-py3-none-any.whl
| Download URL | aiofortiosapi-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
202b158f8cbd7ce4df9b9e52a7a3a4c5f2c3ef9175bbb06d533e0c81a6f12b53
|
|
BLAKE2b-256 checksum How to use checksums |
21a9cb510fc2ec9fceea726ce05a959b3e32c4b9b588b587b648abc6603ccdff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Aug 26, 2026.
Transparency log