Automating FortiGate firewalls with the FortiOS REST API and Python
In short: FortiGate firewalls expose a REST API for configuration and monitoring. You create a REST API administrator with a token and trusted source addresses, then send HTTPS requests with the token. Configuration lives under /api/v2/cmdb/ and live status under /api/v2/monitor/. Python scripts can list or create objects such as addresses, saving time and reducing typing errors.
Why use the API?
Adding 50 address objects or checking firewall status on 30 branches through the GUI is slow and error-prone. The FortiOS REST API lets scripts do it consistently. FortiManager has its own API for central management; this note covers a single FortiGate.
Create a REST API administrator
In the GUI: System, Administrators, Create New, REST API Admin. Choose a profile with only the permissions needed, set trusted hosts to your automation server's address, and copy the generated token (shown once). Store it in a secrets manager or environment variable.
Reading system status
import os
import requests
FGT = "https://10.0.0.254"
HEADERS = {"Authorization": f"Bearer {os.environ['FGT_TOKEN']}"}
CA = "/etc/ssl/certs/company-ca.pem" # the CA that signed the FortiGate's certificate
r = requests.get(f"{FGT}/api/v2/monitor/system/status",
headers=HEADERS, verify=CA, timeout=10)
r.raise_for_status()
print(r.json()["results"])
Listing address objects
r = requests.get(f"{FGT}/api/v2/cmdb/firewall/address",
headers=HEADERS, verify=CA, timeout=10)
for obj in r.json()["results"]:
print(obj["name"], obj.get("subnet"))
Creating an address object
new_obj = {"name": "srv-erp-01", "subnet": "10.50.1.10 255.255.255.255",
"comment": "Created by automation"}
r = requests.post(f"{FGT}/api/v2/cmdb/firewall/address",
headers=HEADERS, json=new_obj, verify=CA, timeout=10)
if r.status_code == 200:
print("Created")
else:
print("Failed:", r.status_code, r.text)
Use PUT to update an existing object at /api/v2/cmdb/firewall/address/srv-erp-01, and DELETE to remove it. Add ?vdom=name when working with VDOMs.
Good practice
- Least-privilege API profiles: read-only for reporting scripts.
- Trusted hosts set to automation servers only.
- Verify TLS certificates; use
verify=Falseonly in a lab. - Check object names before creating, to avoid duplicates.
- Review policy changes made via API in the same way as manual changes.
- Check the API reference for your FortiOS version on the Fortinet developer network; fields can change.
Real-world example: onboarding servers
A hosting team created dozens of address objects and groups each week for new customer servers, with occasional typos causing outages. A script that reads server details from the provisioning system and creates objects through the FortiOS API made the task take seconds and removed the typos.
Practice
What is the difference between cmdb and monitor endpoints in FortiOS?
How should a REST API admin's access be restricted?
How do you specify a VDOM in an API request?
Key takeaways
- FortiOS offers a REST API for configuration and monitoring.
- Use a REST API admin with a token, least privilege and trusted hosts.
- cmdb endpoints manage configuration; monitor endpoints report live state.
- Verify TLS, avoid duplicates and review API changes like manual ones.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.