API Plugin is a component to define a function template for the API Parser and API Server to work with a third-party system.

To define a new API plugin, complete the following steps.

Tip: The system provides a built-in ServiceNow API Plugin and you can refer to it to write your own ones in the API Plugin Manager.

1.Log into System Management page.

2.Select the API Plugin Manager tab and click Add.

3.Enter a name in the Plugin Name field, such as ServiceNow Incidents.

4.Enter a description of the API plugin in the Description field.

5.Enter the API script in the Script field. The sample code can be:

import requests
import json
import urllib3
  
urllib3.disable_warnings()
 
class APIPlugin:
    _headers = {"Content-Type""application/json",
                "Accept""application/json"}
  
    def __init__(self, user, pwd, url):
        self._user = user
        self._pwd = pwd
        self._url = url
        self.auth = (self._user, self._pwd)
 
    # API Plugin Test function definition. The sample test function random retrieves an incident number from ServiceNow to verify the connection.
    def _test(self):
        param = {
            'path''/api/now/table/incident?sysparm_query=&sysparm_display_value=&sysparm_fields=number&sysparm_limit=1'
        }
        result = str(self._get(param))
        return result
 
    #Define a customized RESTful Get function template.         
    def _get(self, param):
        full_url = self._url + param['path']
        urlParams = {}
        if 'urlParams' in param:
            urlParams=param['urlParams']
        try:
            response = requests.get(url=full_url, params=urlParams, headers=self._headers, auth=self.auth, verify=False)
            if response.status_code == 200:
                result = response.json()["result"]
                return result
            else:
                return "Error! " + response.text
        except Exception as e:
            return str(e)
 
def extract_param(param):
    # The NetworkBrain initial parameters with customized fields.
    if isinstance(param, str):
        param = json.loads(param)
 
    #username, password, endpoint are build-in keywords in initial param.
    username = ''
    password = ''
    endpoint = ''
    #callParam is customized fields.
    callParam = {}
 
    if 'username' in param:
        username = param['username']
    if 'password' in param:
        password = param['password']
    if 'endpoint' in param:
        endpoint = param['endpoint']
    if 'callParam' in param:
        callParam = param['callParam']   
 
    return (username, password, endpoint, callParam)
 
# API Domain Manager Test function definition.
def _test(param):
    username, password, endpoint, callParam = extract_param(param)
    api = APIPlugin(username, password, endpoint)
    result = api._test()
    #"isFailed" and "msg" key fileds are the required.
    rtn={"isFailed":False"msg":result}
    return json.dumps(rtn)
 
# Public function for API parser.
def getData(param):
    username, password, endpoint, callParam = extract_param(param)
    ap = APIPlugin(username, password, endpoint)
    rtn = ap._get(callParam)
    return json.dumps(rtn)

Tip: You can click Popup to prompt a larger script interface. Alternatively, you can click Import to import an existing python file directly.

7.Click Save to save the definition.

 

See also:

Adding an API Server