<< Click to Display Table of Contents >> Navigation: Screen Reference > Administration > System Configuration > Using The Web API |
On the API tab in the System Configuration screen you can turn on API access to FastMaint for third-party applications. Note that the API is only available over the web (http: or https: protocols) and requires use of JavaScript Object Notation (JSON). Check the API tab for the URL to use for API access.
Visit the Support Center or contact us for more information on using the Web API.
NOTE: FastMaint does implement throttling to prevent too many API requests at a time. This is done to avoid performance issues or bottlenecks for other users if there are too many requests.
Example 1: Retrieve statistics (Statistics Report data)
# Python 3 code to access JSON API
import requests
import json
import numpy
# example settings - yours will differ
myapikey = "(your API key)" # key from API tab in System Configuration
mytenantkey = "(your subscription key)" # leave empty if FastMaint Web, enter your subscription key if FastMaint Cloud
endpoint = "http://myserver/fastmaint/webapi_v1.ashx" # use web address (URL) on API tab in System Configuration
headers = {}
headers["Content-Type"] = "application/json; charset=utf-8"
# request statistics
myrequest = json.dumps({"tenantkey":mytenantkey,
"apikey":myapikey,
"request":"statistics"})
result = requests.post( url = endpoint, data = myrequest, headers = headers )
# extract results
myret = json.loads(result.text)
# check status
istatus = int(myret["statuscode"])
# export to csv file
if istatus == 0 : # no errors or warnings
print(myret["statusdesc"]) if myret["statusdesc"] == "OK" else print("Warnings! " + myret["statusdesc"])
numrows = int(myret["numrows"])
numcols = int(myret["numcols"])
if numrows > 0 : # data returned
table1d = numpy.asarray(myret["tabledata"])
table2d = table1d.reshape(numrows,numcols)
numpy.savetxt('c:\\temp\\results.csv', table2d, delimiter=',', fmt='"%s"')
print("Data saved to file")
else:
print("No data returned")
else: # warnings or errors
print("Error: " + myret["statusdesc"])
Example 2: Retrieve all work orders in a period
# Python 3 code access JSON API
import requests
import json
import numpy
# example settings - yours will differ
myapikey = "(your API key)" # key from API tab in System Configuration
mytenantkey = "(your subscription key)" # leave empty if FastMaint Web, enter your subscription key if FastMaint Cloud
endpoint = "http://myserver/fastmaint/webapi_v1.ashx" # use web address (URL) on API tab in System Configuration
headers = {}
headers["Content-Type"] = "application/json; charset=utf-8"
# request work orders between a period
myrequest = json.dumps({"tenantkey":mytenantkey,
"apikey":myapikey,
"request":"export-work-order",
"numcols":2,"numrows":1,
"tabledata":["2019/12/01","2020/01/31"]})
result = requests.post( url = endpoint, data = myrequest, headers = headers )
# extract results
myret = json.loads(result.text)
# check status
istatus = int(myret["statuscode"])
# export to csv file
if istatus == 0 : # no errors or warnings
print(myret["statusdesc"]) if myret["statusdesc"] == "OK" else print("Warnings! " + myret["statusdesc"])
numrows = int(myret["numrows"])
numcols = int(myret["numcols"])
if numrows > 0 : # data returned
table1d = numpy.asarray(myret["tabledata"])
table2d = table1d.reshape(numrows,numcols)
numpy.savetxt('c:\\temp\\results.csv', table2d, delimiter=',', fmt='"%s"')
print("Data saved to file")
else:
print("No data returned")
else: # warnings or errors
print("Error: " + myret["statusdesc"])
Example 3: Import alarm readings
# example Python 3 code access JSON API
import requests
import json
import numpy
# example settings - yours will differ
myapikey = "(your API key)" # key from API tab in System Configuration
mytenantkey = "(your subscription key)" # leave empty if FastMaint Web, enter your subscription key if FastMaint Cloud
endpoint = "http://myserver/fastmaint/webapi_v1.ashx" # use web address (URL) on API tab in System Configuration
headers = {}
headers["Content-Type"] = "application/json; charset=utf-8"
# request to import alarm data, first 'row' contains column names, subsequent 'rows' contains alarm readings
myrequest = json.dumps({"tenantkey":mytenantkey,
"apikey":myapikey,
"request":"import-alarm",
"numcols":4,"numrows":2,
"tabledata":[
"Equipment","DateFrom","DateTo","Reason",
"EPJ #1","2019/12/11","2019/12/11","Testing 1"
]})
result = requests.post( url = endpoint, data = myrequest, headers = headers )
# extract results
myret = json.loads(result.text)
# check status
istatus = int(myret["statuscode"])
# export to csv file
if istatus == 0 : # no errors or warnings
print(myret["statusdesc"]) if myret["statusdesc"] == "OK" else print("Warnings! " + myret["statusdesc"])
numrows = int(myret["numrows"])
numcols = int(myret["numcols"])
if numrows > 0 : # data returned
table1d = numpy.asarray(myret["tabledata"])
table2d = table1d.reshape(numrows,numcols)
numpy.savetxt('c:\\temp\\results.csv', table2d, delimiter=',', fmt='"%s"')
print("Data saved to file")
else:
print("No data returned")
else: # warnings or errors
print("Error: " + myret["statusdesc"])
Example 4: Import meter readings
# example Python 3 code access JSON API
import requests
import json
import numpy
# example settings - yours will differ
myapikey = "(your API key)" # key from API tab in System Configuration
mytenantkey = "(your subscription key)" # leave empty if FastMaint Web, enter your subscription key if FastMaint Cloud
endpoint = "http://myserver/fastmaint/webapi_v1.ashx" # use web address (URL) on API tab in System Configuration
headers = {}
headers["Content-Type"] = "application/json; charset=utf-8"
# request to import meter readings, first 'row' contains column names, subsequent 'rows' contains meter readings
myrequest = json.dumps({"tenantkey":mytenantkey,
"apikey":myapikey,
"request":"import-meter",
"numcols":3,"numrows":13,
"tabledata":[
"Equipment","ReadingDate","Reading",
"EPJ #1","2019/12/11","100",
"EPJ #1","2019/12/13","150",
"EPJ #2","2019/12/01","9200",
"EPJ #1","2019/12/14","160",
"EPJ #1","2019/12/15","161",
"EPJ #2","2019/12/08","9300",
"EPJ #1","2019/12/16","170",
"EPJ #1","2019/12/20","180",
"EPJ #1","2020/01/01","200",
"EPJ #1","2019/12/17","175",
"EPJ #1","2020/01/12","220",
"EPJ #2","2019/12/13","150"
]})
result = requests.post( url = endpoint, data = myrequest, headers = headers )
# extract results
myret = json.loads(result.text)
# check status
istatus = int(myret["statuscode"])
# export to csv file
if istatus == 0 : # no errors or warnings
print(myret["statusdesc"]) if myret["statusdesc"] == "OK" else print("Warnings! " + myret["statusdesc"])
numrows = int(myret["numrows"])
numcols = int(myret["numcols"])
if numrows > 0 : # data returned
table1d = numpy.asarray(myret["tabledata"])
table2d = table1d.reshape(numrows,numcols)
numpy.savetxt('c:\\temp\\results.csv', table2d, delimiter=',', fmt='"%s"')
print("Data saved to file")
else:
print("No data returned")
else: # warnings or errors
print("Error: " + myret["statusdesc"])
----------------------------------------------------------------