Fortimail - Rest Api Reference
Fortimail - Rest Api Reference
Version 6.2.0
FORTINET DOCUMENT LIBRARY
https://docs.fortinet.com
FORTINET BLOG
https://blog.fortinet.com
NSE INSTITUTE
https://training.fortinet.com
FORTIGUARD CENTER
https://fortiguard.com/
FEEDBACK
Email: [email protected]
Change Log 4
Introduction 5
Enabling REST API support 6
Authentication 7
PKI authentication 8
To use PKI admin authentication 8
Password-based authentication 9
FortiMail REST API HTTP response codes 10
REST API for system level resources 11
REST API for domain level resources 12
REST API for administrative actions 13
REST API for file upload 14
Require file uploading authorization token 14
Upload local file with authorization token 14
Apply uploaded file on resource 15
Update firmware 15
Update configuration 15
Restore Block/Safe list 15
REST API for IP group management 17
Create a new IP group 17
Delete an IP group 17
Add a new member to an IP group 17
Delete a member from an IP group 18
Replace all members of an IP group 18
System resource list and URLs 19
Example commands 25
Admin login with PKI certificate-based authentication 25
Admin login with password-based authentication 25
To get domain information 25
To get domain information with password-based authentication 25
To get domain information with certificate-based authentication 25
Access control rule management 25
List access control rules 27
Create a new access control rule 27
Modify an existing access control rule 27
Delete an existing access control rule 27
Move existing access control rule 27
Whitelist/blacklist management 28
This document provides the REST API information supported in FortiMail version 5.3.4 release. This document covers
the FortiMail GUI supported REST API reference only. These APIs can be used to retrieve, create, update and delete
configuration settings, to retrieve dynamic system statistics, and to perform basic administrative actions such as reboot
and shut down.
When using the APIs, the following conventions are followed:
l Http GET: To retrieve all resources or particular resource
l Http POST: To create a new resource or perform certain administrative actions
l Http PUT: To update an existing resource
l Http Delete: To delete an existing resource
By default, this feature is disabled on FortiMail. To enable it, use the following CLI command:
config system global
set rest-api enable
end
When making requests to FortiMail appliance using the REST API, you will need to pass the authentication. There are
two authentication options you can use:
l PKI certificate-based authentication
l Local user password-based authentication
You also need the appropriate admin profile to access the FortiMail resources.
For PKI certificate-based authentication, you must create two certificates using the same CA. One certificate will be
used for FML HTTP server and the other certificate will be associated with an admin user. Both the user certificates and
private key has to be moved to the PC running the script. The CA certificate needs to be copied to the PC as well.
Otherwise CURL will not be able to verify the FML certificate.
To establish a valid authentication session, you must make a POST request to the FortiMail login handler with your
admin username and password. The POST request should contain JSON data with ‘name’ and ‘password’ fields:
l URL: http(s)://host_or_ip/api/v1/AdminLogin/
l Method: POST
l JSON: {“name”: “admin”, “password”: “****”}
If login is successful, the response will contain the authentication token in the APSCOOKIE cookie value. This cookie
value must be included in any further requests.
The permissions for the administrative account you use will affect which objects and
operations you'll have access to, so ensure the user has the permissions required for the
actions you wish to perform.
FortiMail REST APIs use well-defined HTTP status codes to indicate query results to the API. Following are some of the
HTTP status codes used:
403 - Forbidden Request is missing authentication token or administrator is missing access profile
permissions.
405 - Method Not Allowed Specified HTTP method is not allowed for this resource.
FortiMail supports retrieval and modification of system level CMDB configuration settings as well as system level
statistics. The API can be accessed using the following URL:
http(s)://host_ip/api/v1/resource_name/resource_id/sub_resource_name/sub_resource_
id/
where:
l resource_name: Specifies the type of resource to query (such as SysInterface), required.
l resource_id: Unique ID of the resource as specified by resource_name (such as port1), optional. If not present,
returns entire list of resources.
l sub_resource_name: Some resources may have sub / child resources, use this to query sub resources,
optional.
l sub_resource_id: Unique ID of the sub resource as specified by sub_resource_name, optional. If not present,
returns entire list of sub resources.
Examples:
l …/api/v1/SysInterface/: Returns list of network interfaces
l …/api/v1/SysInterface/port1/: Return details of network interface ‘port1’
l …/api/v1/SysGlobal/: Returns details of global settings (only one instance)
l …/api/v1/ProfSession/inbound/ ProfSessionSenderWhitelist/: Returns sender whitelist/saftlist
of session profile ‘inbound’
For a full list of system level resources, refer to the Supported Resources List.
FortiMail also supports retrieval and modification of domain level CMDB configuration settings. The API can be
accessed using the following URL:
http(s)://host_ip/api/v1/domain/domain_name/resource_name/resource_id/sub_r
esource_name/sub_resource_id/
It is very similar to the URL for system level resources, only two new tokens are added:
l domain: Required keyword, use to perform domain level queries
l domain_name: FQDN name of the domain to query (such as fortinet.com)
Examples:
l …/api/v1/domain/abc.com/ProfAntispam/: Returns list of antispam profiles for domain ‘abc.com’
l …/api/v1/domain/abc.com/PolicyRcpt/: Returns list of recipient based policies for domain ‘abc.com’
l …/api/v1/domain/abc.com/PolicyRecipient/1/: Returns details of recipient based policy ‘1’ for
domain ‘abc.com’
l …/api/v1/domain/abc.com/UserPreference/: Returns list of entries in user preferences for domain
‘abc.com’
For a full list of domain level resources, refer to the Supported Resources List.
Apart from resources, FortiMail REST API supports basic administrative actions such as restarting / shutting down a
device. Use the following URL to send action request:
l URL: http(s)://host_ip/api/v1/SysStatusCommand/
l Method: POST
l JSON: {“action”: action_value}
Where action_value is one of the following integers:
1: Restart
2: Shut down
3: Reload
There are three steps to upload a file to FortiMail through the REST API:
1. Require file uploading authorization token
2. Upload local file with authorization token
3. Apply uploaded file on resource
/resource/ POST Require authorization token. “Resource” is the name of the resource
on which the uploaded file will apply to. The request must set the
“reqAction” field to 22.
Example:
curl –v –H "Content-Type: application/json" -X POST -d '{"reqAction":22}’ http://host_or_
ip/api/v1/CentralRestoreFirmware --cookie cookie.txt
The “reqAction” field should be set to 22, which requires FortiMail to generate the file uploading authorization token.
“CentralRestoreFirmware” is the ID of the resource on which the uploaded file will apply to. For resources that support
sub resources, extra sub resource IDs should be added, which can always be set to “0”. For example, resource ID
“UserMaillist” supports sub resources, with an example URL of http://host_or_api/api/v1/UserMaillist/0
Response:
{“token”: “tokenStr”}
The “token” value is generated by FortiMail. This is used in the next step.
Example:
curl -F 'testfile=@/tmp/file.diff'
http://host_or_ip/api/v1/uploadfile?token=tokenStr--cookie cookie.txt
In this example, “testfile” is a self-assigned ID to indicate the uploading action, “/tmp/file.diff” is the full path of the local
file you want to upload, and the “token” field should be set to the value provided in the previous step.
Response:
{“id”:”uploadedFileId”}
The “id” value is generated by FortiMail. This is used in the next step.
Update firmware
/CentralRestoreFirmware/ PUT Set the “filename” field to the “id” value from the previous step.
Example:
curl -v -H "Content-Type: application/json" -X PUT -d '{"filename" : "uploadedFileId"}'
http://host_or_ip/api/v1/CentralRestoreFirmware --cookie cookie.txt
Update configuration
/CentralRestoreConfig/ PUT Set the “filename” field to the “id” value from the previous step.
Example:
curl -v -H "Content-Type: application/json" -X PUT –d '
{"source":0,"filename":"uploadedFileId","check_platform":1}' http://host_or_
ip/api/v1/CentralRestoreConfig --cookie cookie.txt
System, domain, and personal block/safe lists can be restored. The resource used for these actions is “UserMaillist”.
Because “UserMaillist” supports sub resources, the URL used in the first step (Require file uploading authorization
token) should have the format, http://host_or_api/api/v1/UserMaillist/0.
/UserMaillist/system PUT System block list restore. Set the “extraParam” field to “blacklist”, and
set the “restore” field to “id”.
/UserMaillist/system PUT System safe list restore. Set the “extraParam” field to “whitelist”, and
/UserMaillist/domainname PUT Domain block list restore. Set the “extraParam” field to “blacklist”, and
set the “restore” field to “id”. “domainname” is the domain on which
the list will restore.
/UserMaillist/domainname PUT Domain safe list restore. Set the “extraParam” field to “whitelist”, and
set the “restore” field to “id”. “domainname” is the domain on which
the list will restore.
/UserMaillist/useraddress PUT Personal block list restore. Set the “extraParam” field to “blacklist”,
and set the “restore” field to “id”. “domainname” is the domain on
which the list will restore.
/UserMaillist/useraddress PUT Personal safe list restore. Set the “extraParam” field to “whitelist”,
and set the “restore” field to “id”. “domainname” is the domain on
which the list will restore.
IP groups and the members of each group can be created, deleted, and managed through the REST API.
REST API only supports IP range values. The IP/netmask format is not accepted.
Delete an IP group
Example:
In the following example, the IP range 172.20.200.11-172.20.200.30 is added to the IP group named group3:
http://ip_or_hostname/api/v1/ProfIp_address_group/group3/ProfIp_address_
groupIpAddressGroupMember/172.20.200.11-172.20.200.30
JSON data:
Example:
The following URL and will clear the member list for the IP group group3, then add two new members:
http://ip_or_hostname/api/v1/ProfIp_address_group/group3/ProfIp_address_
groupIpAddressGroupMember/
JSON data:
{ "reqAction":10,
"reqObjCount":2,
"mkey_0":"172.20.110.10-172.20.110.20",
"mkey_1":"172.20.130.10-172.20.130.30"
}
This section contains various example commands that you may encounter.
curl -v -c cookie.txt --cert test.pem --key test.key --cacert CA.cer -X POST -k https://ip_or_
host/api/v1/AdminLogin
If login is successful, the cookies will be save to cookie.txt, which will be used in the below commands.
If login is successful, the cookies will be save to cookie.txt, which will be used in the below commands.
If you log in to FortiMail with a PKI certificate, you must use both the cookie and certificate together to run the command
sessions. If you log in with user name and password, you only need to use the cookie to run the command sessions. For
example:
Supported values for 'action' attribute of ACL. If not set, the default action is reject:
1 - safe-relay
2 - relay
3 - reject
4 - discard
5 - safe
0 - default / wild-card
1 - regular expression
2 - email group
3 - ldap group
0 - any
1 - authenticated
2 - not-authenticated
0 - ip mask
1 - ip group
Supported values for 'sortingDirection' attribute of applicable JSON requests (i.e. ACL rule):
1 - enabled/sorting
2 - disabled/no sorting
1 - GET
2 - CREATE
3 - DELETE
5 - UPDATET
14 - MOVE
If reqAction is present in JSON, it takes precedence over HTTP method header (i.e. HTTP
GET/POST/PUT/DELETE).
You can move a rule up one place, down one place, before another rule, or after another rule. To move a rule to the top
or bottom, you can find the first. or last rule ID (mkey) and move the rule before the first rule or after the last rule.
reqAction: 14 -- required, only one value: 14 means to move
moveAction : up -- required, 4 values: up/down/before/after
mmkey: 3 – required, ID of the ACL rule to be moved
refMkey: 2 – required, reference ID of the ACL rule when moving before/after this ID
Whitelist/blacklist management