Brain dump for at least semi-good ideas

Fortigate API – FortiOS 6.2

Recently I changed my firewall from Sophos UTM to a Fortigate. Since I have a decent lab setup at home with a bunch of services I decided to try out the Fortigate API. However, to my surprise there was no API documentation openly available online. To get hold of it one had to be a part of the Fortinet Developer Network which requires endorsement from two Fortinet employees. Personally I’m not a bit fan of keeping these things behind closed doors. I think it benefits neither the company, nor the customer.

So in case someone else is in the same situation that I was I thought I’d write a short intro on how to use the API using an admin account using Powershell.


First step is to do send a post against /logincheck using form data:

# Authentication against the box
$PostParameters = @{
    "username" = $FortigateSettings.user;
    "secretkey" = $FortigateSettings.password;

$Result = Invoke-WebRequest -Method POST "" -Body $PostParameters -SessionVariable FortigateSession

The code above also saves the cookies from the response into a session variable called FortigateSession. From this variable we will also extract the CSRFTOKEN cookie value which is required when one wants to change things on the device.

$CSRFTOKEN = ($FortigateSession.Cookies.GetCookies("") | Where-Object { $ -eq "ccsrftoken" }).value.replace("`"", "")

Now we’re set to run commands against the Fortigate API by using the session variable.


# Get the DHCP configuration
Invoke-WebRequest "" -WebSession $FortigateSession

# Get a list of the DNS databases
Invoke-WebRequest "" -WebSession $FortigateSession -Method "GET"

# Get a list of the address objects
Invoke-WebRequest "" -WebSession $FortigateSession

# Add an address object
$SHost = @{
    "name" = "CloudFlare-1";
    "subnet" = "";
} | ConvertTo-Json -Compress

Invoke-WebRequest "" -Headers @{"Content-Type" = "application/json"; "X-CSRFTOKEN" = $CSRFTOKEN} -WebSession $FortigateSession -Method "POST" -Body $SHost -ErrorAction SilentlyContinue

Please note that while these examples covers authentication using a normal admin account the Fortigate devices also has support for dedicated REST accounts using tokens. For frequent/production integrations you’d want to look there instead.

The script I used to migrate from Sophos to Fortigate is available here.


BigIp Report – 2019 Survey


Writing a custom integration for the Google assistant


  1. Ishrat Jahan

    Hi, Can you please tell me the equivalent API for the following CLI commands:
    get system arp
    get hardware status
    get system performance status
    get router info multicast pim sparse-mode interface
    get router info multicast pim sparse-mode rp-mapping

    • Patrik Jonsson

      Sorry, but I do not have the time to figure out the commands for these.

      Kind regards,

Leave a Reply

Your email address will not be published. Required fields are marked *

Powered by WordPress & Theme by Anders Norén