You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 20 Next »

Scripted REST API functionality allows application developers to implement custom web APIs.

Define API actions and request parameters that utilize the customized API logic (including but not limited to: the script implementing method; request path; authentication require; parameter obligate for related modules and actions).

Role required: admin.

Scripted REST API URIs


The format of scripted REST API URIs is represented below:

{your_instance_url}/v1/api/{applicationSlug}/{module_path}/{version}/{action_path}?{params}


URI analysis

URI partDescription
{your_instance_url}Path to the instance where the scripted REST API can be accessed.
{applicationSlug}

This parameter is composite and designated out of the parts below:

{applicationSlug}{application.table_prefix}_{application.name}

The attributes listed above belong to the application within of which the implementing is being conducted.

Non-English application names will be transliterated automatically.

{module_path}API module containing references to essences implementing the customized REST API logic (actions, request parameters, and so on). API modules are described below.
{version}(optional) Version of the API endpoint if versioning is used, for example, v1.
{action_path}API action containing main logic and behavior of your Scripted REST API. API actions are described below.

How to implement your custom API


  1. Create an API module.
  2. Create an API version.
  3. Configure API actions.
  4. Configure API request parameters.
  5. Bind API request parameters with appropriate API modules and actions.

Creating an API Module


API module is a connecting element for API versions and API actions. 

To create an API Module, please complete the steps below:

  1. Navigate to Scripted REST API → API Modules.
  2. Click New and fill in the form.
  3. Click Save or Save and Exit to apply changes.

API Module form fields

FieldDescription
NameSpecify the API module name.
PathRelative path to the API module, for example, 'cmdb.'
ActiveSelect this checkbox to make the module active or inactive. When set to 'false', you'll be unable to choose this module in referencing tables (API Version, API Action, API Module Request Parameters). 

Creating an API Version


Scripted REST API enables versioning, allowing developers to deploy changes without affecting existing integrations.

To configure an API version, please complete the steps below:

  1. Navigate to Scripted REST API → API Versions.
  2. Click New and fill in the fields.
  3. Click Save or Save and Exit to apply changes.

API version form fields

FieldDescription
ModuleSpecify the API module created earlier for this version.
ActiveSelect this checkbox to make the version active or inactive. When set to 'false', you'll be unable to choose this version in referencing tables (API Action).
PathPath to the API version, for example, 'v1'.

The first version, that is v1, ready for deployment, is created automatically when the API module is created.

API requests without a version specified automatically use the last version having Active state.

Configuring an API Action


In there, you can configure an action implementing the logic and behavior of your customized API method.

Your actions created earlier can be reached by URI like:

{your_instance_url}/v1/api/{application}/{module_path}/{action_path}

or

{your_instance_url}/v1/api/{application}/{module_path}/{version}/{action_path}


The second URI sample is used in case of the versioning enabled.

To configure an API action, please complete the steps below:

  1. Navigate to Scripted REST API → API actions.
  2. Click New and fill in the fields.
  3. Click Save or Save and Exit to apply changes.

API action form fields

FieldDescription
NameThe action name.
PathA path for API action.
ActiveSelect this action to make the version active or inactive. When set to 'false', you'll be unable to choose this version in referencing tables (API Action Request Parameter).
Is Authentication Required


Select this checkbox if the request requires authentication before execution. The token passes in the request header. By default, this statement is equal to 'true'.

Only Bearer authentication method is supported. That said, you need to pass the authentication token in the request header.

In the code example below, you can obtain an option how to retrieve the authorization token with user login and password (this code block uses example data for the setRequestUrl method and the payload constant. Replace it within the real usage):

const simpleInstanceUri = ss.getProperty('simple.instance.uri');
const URL_BASE = (simpleInstanceUri.startsWith('https://')) ? simpleInstanceUri : `https://${simpleInstanceUri}`;
const authRequest = sws.restRequestV1();
authRequest.setRequestUrl(`${URL_BASE}/v1/auth/login`);
authRequest.setRequestMethod('POST');
authRequest.setRequestHeader('Content-type', 'application/json');
const payload = {
    "username": "admin",
    "password": "qwerty123456"
}
authRequest.setRequestBody(JSON.stringify(payload));
const authResponse = authRequest.execute();
if (JSON.parse(authResponse.getBody()).status === 'ERROR') {
  ss.error(JSON.parse(authResponse.getBody()).errors[0].message);
  ss.info('Check credentials at 10..11 lines of current script');
  return;
}
ss.info(JSON.parse(authResponse.getBody()).data.auth_key);
// Info: 5WvOT9Ejlxd6Gb8bkCWMtG3XXMYcoDDJ


ModuleChoose the API Module created earlier (activate module before choosing). This field references the API Module (sys_api_module) table.
VersionChoose the API version created earlier (activate version before choosing). This field references the API Version (sys_api_version) table.
HTTP MethodSelect the method this action implements (GET, or POST, for example).
Script

Specify a script implementing your action, using the Server-Side API, SimpleApiRequest and SimpleApiResponse side API classes there.

Configuring API request parameters


In there, you can specify the request parameters, for example, you can make the parameter mandatory for all referenced API essences.

To configure this, please complete the steps below:

  1. Navigate to Scripted REST API → API Request Parameters.
  2. Click New and fill in the fields.
  3. Click Save or Save and Exit to apply changes.

API Request parameter form fields

FieldDescription
NameThe request parameter name.
Is requiredSelect this checkbox to make this parameter mandatory for all referenced API essences.

Configuring API action request parameters


In this section, you can bind specified API module with relevant request parameters.

To configure them, please complete the steps below:

  1. Navigate to Scripted REST API → API Action Request Parameters.
  2. Click New and fill in the fields.
  3. Click Save or Save and Exit to apply changes.

API action request parameters form fields

FieldDescription
ActionSpecify the API action created earlier for which you need to define a related API request parameter. This field referenced the API action (sys_api_action) table.
Request parameterSpecify a request parameter related to this action. When specified, an action referenced to this request parameter can use its options predefined in the API request parameters dictionary. This field references the Request Parameter (sys_api_request_param) table.

Configuring API module request parameters


In this section, you can bind specified API module with relevant request parameters.

To configure this relationship, please complete the steps below:

  1. Navigate to Scripted REST API → API Module Request Parameters.
  2. Click New and fill in the fields.
  3. Click Save or Save and Exit to apply changes.

API Module request parameters form fields

FieldDescription
ModuleSpecify the API module created earlier for which you need to define a related API request parameter. This field references the API Module (sys_api_module) table.
Request parameter

Specify a request parameter related to this module. When specified, all actions referenced to the module above can use this request parameter. This field references the Request Parameters (sys_api_request_param) table.

Example case


Consider a case of implementing Scripted REST API returning data in response to a POST request.

The request looks like: http://your-instance-url.example.com/v1/api/c_simple/api_module_path/api_action_path?param_1=value_1

Your actions should go as follows:

  1. Create an API module. Fill in the fields as given below:
    1. Name  type a module name.   
    2. Path type api_module_path there.
    3. Select the Active checkbox to activate the module.
  2. Create an API version out of API module created earlier. Fill in the fields as given below:
    1. Name type a version name.
    2. Module this field populates automatically with reference to the API module
    3. Path type v1 there,
    4. Select the Active checkbox to activate the version.
  3. Create an API action out of API version created earlier. Fill in the fields as given below:
    1. Name  type an action name.
    2. Path type api_action_path there.
    3. Module  this field populates automatically with reference to the API module.
    4. Version  this field populates automatically with reference to the API version.
    5. HTTP Method select POST in this list.
    6. Select the Active checkbox to activate the version.
    7. Is Authentication Required select this checkbox if you are sending authenticated requests.
    8. Script put a script here given in a code example below .
  4. Send a POST request to the URL given in the introductory. You can use any tool you are comfortable with (cURL, or Postman, or any other one).
    1. Substitute the example domain (your-instance-url.example.com) in the endpoint and in the script body with your real hostname.
  5. Check logs (the sys_log table) to locate responses.


Scripted REST
(function (request, response) {
    ss.info("getBody(): " + JSON.stringify(request.getBody()));
    ss.info("getHeader('accept-encoding'): " + JSON.stringify(request.getHeader('accept-encoding')));
    ss.info("getHeaders(): " + JSON.stringify(request.getHeaders())); // {"accept-encoding":["gzip, deflate, br"],"postman-token":["7803024c-7e21-4e0d-840b-392931597c2c"],"cache-control":["no-cache"],"accept":["*/*"],"user-agent":["PostmanRuntime/7.26.8"],"content-length":["0"],"connection":["close"],"x-nginx-proxy":["true"],"host":["your-instance-url"],"x-real-ip":["XX.XX.XX.XX"],"content-type":[""]}
    ss.info("getQueryParams(): " + JSON.stringify(request.getQueryParams())); // {"param_1":"value_1"}
    ss.info("getQueryString(): " + request.getQueryString()); // param_1=value_1
    ss.info("getUri(): " + request.getUri()); // /v1/api/c_simple/api_module_path/api_action_path?param_1=value_1
    ss.info("getUrl(): " + request.getUrl()); // https://your-instance-url.example.com/v1/api/c_simple/api_module_path/api_action_path?param_1=value_1
    response.setBody({
        "status": "ok",
        "support_phone": ss.getProperty('simple.auth_page.support_phone')
    });
})(SimpleApiRequest, SimpleApiResponse)

  • No labels