Script Includes
REST API Client Utilities
Reusable functions for making REST API calls to external systems with error handling and authentication.
var RestAPIClient = Class.create();
RestAPIClient.prototype = {
/**
* Make a GET request to external API
*
* @param {string} endpoint - Full API endpoint URL
* @param {object} options - {headers, params, timeout}
* @returns {object} {success: boolean, status: number, data: object, error: string}
*/
get: function(endpoint, options) {
return this._makeRequest('GET', endpoint, null, options);
},
/**
* Make a POST request to external API
*
* @param {string} endpoint - Full API endpoint URL
* @param {object} data - Request body data
* @param {object} options - {headers, params, timeout}
* @returns {object} {success: boolean, status: number, data: object, error: string}
*/
post: function(endpoint, data, options) {
return this._makeRequest('POST', endpoint, data, options);
},
/**
* Make a PUT request to external API
*
* @param {string} endpoint - Full API endpoint URL
* @param {object} data - Request body data
* @param {object} options - {headers, params, timeout}
* @returns {object} {success: boolean, status: number, data: object, error: string}
*/
put: function(endpoint, data, options) {
return this._makeRequest('PUT', endpoint, data, options);
},
/**
* Make a DELETE request to external API
*
* @param {string} endpoint - Full API endpoint URL
* @param {object} options - {headers, params, timeout}
* @returns {object} {success: boolean, status: number, data: object, error: string}
*/
delete: function(endpoint, options) {
return this._makeRequest('DELETE', endpoint, null, options);
},
/**
* Internal method to make HTTP request
* @private
*/
_makeRequest: function(method, endpoint, data, options) {
var result = {
success: false,
status: 0,
data: null,
error: ''
};
options = options || {};
try {
// Create REST message
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(endpoint);
request.setHttpMethod(method);
// Set default headers
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Accept', 'application/json');
// Add custom headers
if (options.headers) {
for (var header in options.headers) {
request.setRequestHeader(header, options.headers[header]);
}
}
// Add query parameters to URL
if (options.params) {
var paramString = this._buildQueryString(options.params);
if (paramString) {
endpoint += (endpoint.indexOf('?') === -1 ? '?' : '&') + paramString;
request.setEndpoint(endpoint);
}
}
// Set request body for POST/PUT
if (data && (method === 'POST' || method === 'PUT')) {
var jsonData = typeof data === 'string' ? data : JSON.stringify(data);
request.setRequestBody(jsonData);
}
// Set timeout (default 30 seconds)
var timeout = options.timeout || 30000;
request.setHttpTimeout(timeout);
// Execute request
var response = request.execute();
result.status = response.getStatusCode();
// Parse response body
var responseBody = response.getBody();
if (responseBody) {
try {
result.data = JSON.parse(responseBody);
} catch (e) {
result.data = responseBody; // Return as string if not JSON
}
}
// Check success
if (result.status >= 200 && result.status < 300) {
result.success = true;
} else {
result.error = 'HTTP ' + result.status + ': ' + response.getStatusMessage();
}
// Log request details
gs.debug('RestAPIClient ' + method + ' ' + endpoint + ' - Status: ' + result.status);
} catch (e) {
result.error = 'Request failed: ' + e.message;
gs.error('RestAPIClient error: ' + e.message + ', endpoint: ' + endpoint);
}
return result;
},
/**
* Build query string from parameters object
* @private
*/
_buildQueryString: function(params) {
var parts = [];
for (var key in params) {
if (params[key] !== null && params[key] !== undefined) {
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]));
}
}
return parts.join('&');
},
/**
* Make authenticated request with Bearer token
*
* @param {string} method - HTTP method
* @param {string} endpoint - API endpoint
* @param {object} data - Request data
* @param {string} token - Bearer token
* @param {object} options - Additional options
* @returns {object} Response object
*/
authenticatedRequest: function(method, endpoint, data, token, options) {
options = options || {};
options.headers = options.headers || {};
options.headers['Authorization'] = 'Bearer ' + token;
return this._makeRequest(method, endpoint, data, options);
},
/**
* Make request with Basic authentication
*
* @param {string} method - HTTP method
* @param {string} endpoint - API endpoint
* @param {object} data - Request data
* @param {string} username - Username
* @param {string} password - Password
* @param {object} options - Additional options
* @returns {object} Response object
*/
basicAuthRequest: function(method, endpoint, data, username, password, options) {
options = options || {};
options.headers = options.headers || {};
// Create basic auth header
var credentials = username + ':' + password;
var encoded = GlideStringUtil.base64Encode(credentials);
options.headers['Authorization'] = 'Basic ' + encoded;
return this._makeRequest(method, endpoint, data, options);
},
type: 'RestAPIClient'
};How to use it
1. Create a new Script Include 2. Set Name to "RestAPIClient" 3. Leave "Client callable" unchecked 4. Copy the code above 5. Use in Business Rules or other Script Includes to call external APIs Example usage: var api = new RestAPIClient(); var response = api.get('https://api.example.com/data', {params: {id: '123'}}); if (response.success) { gs.info('Data: ' + JSON.stringify(response.data)); } else { gs.error('Error: ' + response.error); }
Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.