How to read data from API with Python3

4 minute read

To read data from API directly from the IP Fabric platform is very simple. The best way to start is to have a quick look at our documentation. Even for anyone who's never done any testing before. It's very standard and it contains useful information about API endpoint, authentication options or response codes to begin with.

In the previous post, we demonstrated a quick tutorial for Zabbix and Ansible integration with Bash script. The current post is dedicated to authenticating to IP Fabric's API with username and password, get token and use token-based authentication furthermore. And finally to read data from API.

Authenticating to IP Fabric's API

There are two main HTTP authentication methods with only one possible sequence. The first is basic authentication, for which local or LDAP users can be utilized. The second is token-based and to request a token, one has to authenticate with the first method to get the token. That clarifies the sequence order.

Very importantly, any user has to log in via GUI beforehand to accept the End-User License Agreement (EULA). Only then we may begin.

For testing the API we will use standard Python3 (3.6.6) console in Ubuntu (18.04.3 LTS). In Python3 we will abstract all requests with the help of the 'requests' library, which includes all we need.

#> python3
 Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
 [GCC 8.3.0] on linux
 Type "help", "copyright", "credits" or "license" for more information.
import requests
serverName = 'https://ipfabric.domain/api/v1/'
authData = { 'username': 'username', 'password' : 'password' }
authEndpoint = serverName + 'auth/login'
authPost =, json=authData, verify=False)

Testing responses in console

To confirm successful authentication, we may test for 'status_code' or 'reason' property from the response server.

>>> 200
>>> 'OK'
>>> dict_keys(['accessToken', 'refreshToken'])

According to our response, we can tell we have the access and refresh token ready. The access token expires after 30 minutes, it needs to be refreshed before with the refresh token, that expires after 24 hours if not blacklisted.

Read data from API

After successful authentication and with having the accessToken at hand, we can start the data mining from IP Fabric. All technology tables have their own API endpoint, that can be found on request.

We will start with the device inventory. To be able to perform token-based authentication, we will create the 'tokenHeaders' for our request. To get the data, we need to specify the right endpoint and the payload. In the payload we will can define columns, filters or even sorting options.

snapshotId = '$last'
devicesEndpoint = serverName + 'tables/inventory/devices'
devicesPayload = {'columns':["sn","hostname", "vendor"], filters: {}, 'snapshot':snapshotId,}

accessToken = authPost.json()['accessToken']
tokenHeaders = { 'Authorization' : 'Bearer ' + accessToken}

reqDevices =, headers=tokenHeaders, json=devicesPayload, verify=False)

>>> dict_keys(['data', '_meta'])
>>> {'limit': None, 'start': 0, 'count': 633, 'size': 633, 'snapshot': '86c65bbc-f3e2-4ca1-8b9a-f6eb859081ed'}         

The response provides two main dictionary keys: the 'data' (the actual data, list of devices with serial number and login IP) and '_meta' (contains response overview with a number of returned devices or snapshot identification). The full script with simple flow control is below.

Simple python3 script example for authenticating to IP Fabric's API and printing the _meta data from device inventory to the console.
# Built-in/Generic Imports
import requests,sys
# Suppressing SSL certificate warnings if needed

# Starting with variables
serverName = ''
snapshotId = '$last'
authData = { 'username': 'username', 'password' : 'password' }
authEndpoint = serverName + 'auth/login'
devicesEndpoint = serverName + 'tables/inventory/devices'
devicesPayload = {'columns':["sn","hostname", "vendor"],'snapshot':snapshotId,}

print('\n Attempting to authenticate to: ' + authEndpoint)
# Initiating authentication request to obtain authentication token
authPost =, json=authData, verify=False)
if not authPost.ok:
    print('Unable to authenticate: ' + authPost.text)
    print('\n ..Script will exit.\n')

# Collecting the accessToken 
accessToken = authPost.json()['accessToken']
# Creating the tokenHeaders 
tokenHeaders = { 'Authorization' : 'Bearer ' + accessToken}

print('\n Requesting data from API endpoint: ' + devicesEndpoint)
# Contacting the API endpoint for device inventory
reqDevices =, headers=tokenHeaders, json=devicesPayload, verify=False)
if not reqDevices.ok:
    print('Unable to authenticate: ' + reqDevices.text)
    print('\n ..Script will exit.\n')

# Printing _meta response
print('\n Data collected successfully…')
print('\n The _meta data: ' + str(reqDevices.json()['_meta']))
print('\nScript ends. \n ')

Thank you for reading and enjoy the first part of our recording:

If you have found this article helpful, please follow our company’s LinkedIn or Blog, where there will be more content emerging. Furthermore, if you would like to test our platform to evaluate how it can assist you in managing your network more effectively, please let us know through

Get IP Fabric

Request a demo and discover how to increase
your networks visibility & get better time efficiency.
Free Demo | Zero Obligation
Request a Demo
We're Hiring!
Join the Team and be part of the Future of Network Automation
Available Positions
IP Fabric, Inc.
115 BROADWAY, 5th Floor
NEW YORK NY, 10006
United States
This is a block of text. Double-click this text to edit it.
Phone : +1 (914) 752-2991
Email : [email protected]
IP Fabric s.r.o.
Kateřinská 466/40
Praha 2 - Nové Město, 120 00
Czech Republic
This is a block of text. Double-click this text to edit it.
Phone : +420 720 022 997
Email : [email protected]
IP Fabric, Inc. © 2022 All Rights Reserved