When building Suitelets, you may occasionally need to capture the IP address of the user accessing the page—whether for security auditing, regional customization, or analytics and tracking purposes. Fortunately, NetSuite includes the client’s IP address in the request headers when the Suitelet is accessed through a web browser.
By referencing the ns-client-ip header, you can easily extract and log the IP address of the user in your script.
This technique can be valuable for:
Identifying suspicious or unauthorized access.
Logging IPs for compliance or audit trails.
Tailoring content or services based on user location.
Note: This method only works when the Suitelet is accessed via a browser.
Here’s a quick example using SuiteScript 2.x:
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
define(['N/ui/serverWidget'], function(widget) {
function onRequest(context) {
// Retrieve the IP address from the request headers
context.request.headers['ns-client-ip'];
// Log the entire headers object (optional)
log.debug({ title: 'Request Headers', details: context.request.headers });
// Log the IP address
log.debug({ title: 'Client IP Address', details: ip });
}
return {
onRequest: onRequest
};
});