Create an Authentication Header Using a Secure String

The following sample shows how to use a Suitelet to create an authentication header and send the request to a service (which requires an authentication header) using an https.SecureString. For more information about SecureString, see https.SecureString.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

          /**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
 
// This script creates an authentication header using an https.SecureString.
define(['N/https', 'N/encode'], (https, encode) => {
    function onRequest(context) {

        // Secrets with these two Script IDs must be existing and allowed for this script
        const nameToken = "custsecret_myName";
        const passwordToken = "custsecret_mypPassword";

        // Create BASE-64 encoded name:password pair
        const secStringKeyInBase64 = https.createSecureString({
            input: "{" + nameToken + "}:{" + passwordToken + "}"
        });

        secStringKeyInBase64.convertEncoding({
            toEncoding: encode.Encoding.BASE_64,
            fromEncoding: encode.Encoding.UTF_8
        });

        // Construct the Authorization header
        const secStringBasicAuthHeader = https.createSecureString({
            input: "Basic "
        });

        secStringBasicAuthHeader.appendSecureString({
            secureString: secStringKeyInBase64,
            keepEncoding: true
        });

        // Send the request to third party with the Authorization header
        const resp = https.get({
            url: "myUrl",
            headers: {
                "Authorization": secStringBasicAuthHeader
            }
        });
    };
    return {
        onRequest: onRequest
    };
}); 

        

General Notices