API Example

iNode offers two authentication methods, OAuth2 and API token. The following shows two different approach for each authentication method.

OAuth2 Authentication #

You can use the example below if your authentication method is OAuth2. If you are using the API Key method, please look at the next example.

Python #

import requests
from time import sleep

USERNAME = '<company owner username>'
PASSWORD = '<company owner password>'
CLIENT_ID = '<client id>'
CLIENT_SECRET = '<client secret>'
SCOPE = 'sync'

BASE_URL = "https://inode.smatstraffic.com/api/"
AUTH_URL = BASE_URL + "oauth/token/"
V1_URL = BASE_URL + "v1/"
LINKS_URL = V1_URL + "links/"
DEVICES_URL = V1_URL + "devices/"

auth_body = {
    'grant_type': 'password',
    'username': USERNAME,
    'password': PASSWORD,
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
    'scope': SCOPE
}
# content-type must be application/x-www-form-urlencoded
auth_response = requests.post(AUTH_URL, data=auth_body)

if auth_response.status_code != 200:
    print(auth_response.json())
    print('failed to authenticate')
    exit(-1)

auth_response_json = auth_response.json()
access_token = auth_response_json['access_token']
refresh_token = auth_response_json['refresh_token']

authorized_header = {
    'Authorization': 'Bearer %s' % access_token
}

def get_new_token(refresh_tok):
    global authorized_header, access_token, refresh_token
    auth_refresh_body = {
        'grant_type': 'refresh_token',
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'refresh_token': refresh_tok
    }
    auth_response = requests.post(AUTH_URL, data=auth_refresh_body)

    if auth_response.status_code != 200:
        print('failed to refresh token')
        exit(-1)

    auth_response_json = auth_response.json()
    # retrieve new access_token
    access_token = auth_response_json['access_token']
    refresh_token = auth_response_json['refresh_token']
    # update the header with a new access token
    authorized_header = {
        'Authorization': 'Bearer %s' % access_token
    }


# retrieve list of links and devices(sensors) in a loop (every minute)
while True:
    # retrieve the list of the links (without pagination)
    links_response = requests.get(LINKS_URL + '?no_pagination', headers=authorized_header)
    if links_response.status_code == 401:
        # access token gets expired after 5 days
        # so we can use refresh_token to get a new access_token
        get_new_token(refresh_token)
        # new access_token is retrieved - continue the loop
        continue
    links_response_json = links_response.json()
    # print list of all links in your company
    print(links_response_json)

    # retrieve the list of the devices (without pagination)
    devices_response = requests.get(DEVICES_URL + '?no_pagination', headers=authorized_header)
    devices_response_json = devices_response.json()
    # print list of all devices in your company
    print(devices_response_json)

    sleep(60)

API Key #

If you have retrieved your API Key from iNode > Settings > Profile > API Access (owner only) page, you can use the following example.

Python #

import requests

BASE_URL = "https://inode.smatstraffic.com/api/"
AUTH_URL = BASE_URL + "oauth/token/"
V1_URL = BASE_URL + "v1/"
LINKS_URL = V1_URL + "links/"
DEVICES_URL = V1_URL + "devices/"

API_ACCESS_KEY = '<---api key retrieved from iNode profile page of the owner--->'

header = {
    'Authorization': 'Token %s' % API_ACCESS_KEY
}

# retrieve list of links and devices(sensors) in a loop (every minute)
while True:
    # retrieve the list of the links (without pagination)
    links_response = requests.get(LINKS_URL + '?no_pagination', headers=header)
    links_response_json = links_response.json()
    # print list of all links in your company
    print(links_response_json)

    # retrieve the list of the devices (without pagination)
    devices_response = requests.get(DEVICES_URL + '?no_pagination', headers=header)
    devices_response_json = devices_response.json()
    # print list of all devices in your company
    print(devices_response_json)

    sleep(60)