FortiGate
FortiGate Next Generation Firewall utilizes purpose-built security processors and threat intelligence security services from FortiGuard labs to deliver top-rated protection and high performance, including encrypted traffic.
yuj_FTNT
Staff
Staff
Article Id 405986
Description This article demonstrates how to access a FortiGate via REST API using a Python script.
Scope FortiGate.
Solution

The REST API token needs to be obtained from the FortiGate. Below is the link that has instructions on how to obtain a REST API token: FortiGate 7.4.8 REST API administrator Guide

 

The script below is an example to retrieve system status, which is equivalent to 'get system status'.

 

import requests
import json

 

url = "https://FGT IP/api/v2/monitor/system/status"

payload = ""
headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer [API Token]'   <-----  Enter the token obtained from the previous step. Note that the Bearer should be kept and after that, the API Token should be added.
}

response = requests.request("GET", url, headers=headers, data=payload, verify=False)

output = response.json()

print(json.dumps(output, indent=4)) 
 

The URL can be changed to retrieve different information from the FortiGate. It is recommended to use the certificate by removing 'verify=False'. 

 

Below are the results of the script.

 

{
    "http_method": "GET",
    "results": {
        "model_name": "FortiGate",
        "model_number": "71F",
        "model": "FGT71F",
        "hostname": "Office-FGT",
        "log_disk_status": "available"
    },
    "vdom": "root",
    "path": "system",
    "name": "status",
    "status": "success",
    "serial": "FortiGate Serialxxxx",
    "version": "v7.4.8",
    "build": 2795
}