How to populate location based on Rest API/API Key

Rajasekhar Redd
Tera Contributor

 

I have a complex scenario with three different APIs, as shown below; I have to populate the location based on the authentication token of the REST API.

API 1 for NOAM location (/api/laed/api1_event_noam)

API 2 for APAC location (/api/laed/api2_event_apac)
API 3 for EMEA location (/api/laed/api3_event_emea)

 

I have written script like below
var test_url = gs.getProperty('test.api.url');
var auth_token = 'Bearer ' + gs.getProperty('test.auth_token_us');

 

As a colleague familiar with API integration and scripting, could you please share any suggestions or ideas for the script?

1 REPLY 1

Abhay Kumar1
Giga Sage

@Rajasekhar Redd You can create a function that determines the API endpoint and token based on the region and then makes the REST API call.

Here is an example script, please feel free to add/amend and you can comment all log.

(function executeAPI() {

    // Get system properties or define them as required

    var noamToken = 'Bearer ' + gs.getProperty('test.auth_token_noam');

    var apacToken = 'Bearer ' + gs.getProperty('test.auth_token_apac');

    var emeaToken = 'Bearer ' + gs.getProperty('test.auth_token_emea');

    var locationApi = {

        NOAM: '/api/laed/api1_event_noam',

        APAC: '/api/laed/api2_event_apac',

        EMEA: '/api/laed/api3_event_emea'

    };

    // Function to get location based on region

    function getLocation(region) {

        var endpoint = locationApi[region];

        var token = getToken(region);

        if (!endpoint || !token) {

            gs.error("Invalid region or missing token for: " + region);

            return;

        }

        var request = new sn_ws.RESTMessageV2();    request.setEndpoint(gs.getProperty('test.api.url') + endpoint);

        request.setHttpMethod('GET'); // Adjust based on your API  request.setRequestHeader('Authorization', token);

        try {

            var response = request.execute();

            var responseBody = response.getBody();

            var httpStatus = response.getStatusCode();

            if (httpStatus === 200) {

                var parsedResponse = JSON.parse(responseBody);

                return parsedResponse.location; // Adjust to match API response

            } else {

                gs.error("API call failed for " + region + ". HTTP Status: " + httpStatus);

                return null;

            }

        } catch (ex) {

            gs.error("Error while calling API for " + region + ": " + ex.message);

            return null;

        }

    }

    // Function to determine the token based on the region

    function getToken(region) {

        switch (region) {

            case 'NOAM':

                return noamToken;

            case 'APAC':

                return apacToken;

            case 'EMEA':

                return emeaToken;

            default:

                return null;

        }

    }

    // Example usage

    var region = 'NOAM'; // Dynamic region selection logic can be added

    var location = getLocation(region);

    if (location) {

        gs.info("Location for " + region + ": " + location);

    } else {

        gs.

error("Failed to fetch location for region: " + region);

    }

})();

 

The script uses GET as the HTTP method. Modify it to POST, PUT, etc., if required.

Store the mapping of regions to endpoints in ServiceNow properties to make it easier to manage.

You can also add retry logics if needed.

Hope this will help you.