Fortinet white logo
Fortinet white logo

Python examples

Python examples

The following snippets of Python code provide examples of how to use the FortiPortal API.

Basic setup (use this code for each example)

The following example shows how to log in to the API, and retrieve the session ID from the response message:

url_login="https://10.16.0.42/fpc/api/login"

client = requests.session()

#Login request

payload = {'user' : 'myuser', 'password' : 'mypass'}

r = client.post(url_login, json=payload, verify=False )

#Retrieve session id. Add to HTTP header for future messages

parsed_json = json.loads(r.text)

sid = parsed_json['fpc-sid']

headers = {'fpc-sid' : sid }

Retrieve a collection

The following example shows how to gather all customer users for customer index 1:

url_cust_req="https://10.16.0.42/fpc/api/customers/1/users"

r = client.get(url_cust_req, headers=headers, verify=False)

Retrieve an object

The following example shows how to obtain device index 3:

url_cust_req="https://10.160.14.103/fpc/api/devices/3"

r = client.get(url_cust_req, headers=headers, verify=False)

Python examples

Python examples

The following snippets of Python code provide examples of how to use the FortiPortal API.

Basic setup (use this code for each example)

The following example shows how to log in to the API, and retrieve the session ID from the response message:

url_login="https://10.16.0.42/fpc/api/login"

client = requests.session()

#Login request

payload = {'user' : 'myuser', 'password' : 'mypass'}

r = client.post(url_login, json=payload, verify=False )

#Retrieve session id. Add to HTTP header for future messages

parsed_json = json.loads(r.text)

sid = parsed_json['fpc-sid']

headers = {'fpc-sid' : sid }

Retrieve a collection

The following example shows how to gather all customer users for customer index 1:

url_cust_req="https://10.16.0.42/fpc/api/customers/1/users"

r = client.get(url_cust_req, headers=headers, verify=False)

Retrieve an object

The following example shows how to obtain device index 3:

url_cust_req="https://10.160.14.103/fpc/api/devices/3"

r = client.get(url_cust_req, headers=headers, verify=False)