Home » Deeper in to NetSuite » NetSuite Secure File Transfer Protocol (SFTP) Module

NetSuite Secure File Transfer Protocol (SFTP) Module

SuiteScript API 2016.2 has introduced a new feature, which enables a direct connection between SFTP servers and NetSuite. Skipping the middleware server previously required for SuiteScript API 1.0 has the advantage of not only decreased dependency but also a more efficient and timely process. However, this new model does have some shortcomings, such as:

  • The inability to list the contents of a remote directory
  • The requirement of a supported server for encryption algorithms and the consequent inability to support all types of servers
  • Ineffectiveness connecting to FTP or FTPS
  • Lack of access to the server’s host key in the NetSuite environment
CTA - NetSuite Joor Connector Banner

NetSuite has approved the algorithms AES256-ctr, AES192-ctr, and AES128-ctr for connectivity. Therefore, the server can use the following encryption algorithms

  • AES 128-CTR
  • AES 192-CTR
  • AES 256-CTR

Using the SFTP module

The following steps will help connect to an SFTP server using the SFTP module:

Generate the host-key of SFTP server

The SFTP server’s host key can be generated through the following command:

Ssh-keyscan hostname or IP address

CTA - NetSuite Implementation Banner

Generate GUID in NetSuite

The Global Unique Identifier can be generated in NetSuite using a particular algorithm. Through this algorithm, a GUID can be generated for a particular domain or script.

An example of a sample code snippet for generating a GUID is the following:


define(['N/ui/serverWidget',N/log"],
function (serverWidget, log) {
function onRequest(context) {
if (context.request.method === 'GET') {
var form = serverWidget.createForm({
title: 'Guid Form’
});
form.addField({
id: 'username',
type: serverWidget.FieldType.TEXT,
label: 'Username'
});
form.addCredentialField({
id: 'password',
label: 'Password',
restrictToScriptIds: 'customscript_id, //id of Script Using sftp
restrictToDomains: ‘domain name'
});
form.addSubmitButton({
label: 'Submit Button'
});
context.response.writePage(form);
return;
} else {
var requset = context.request;
var myPwdGuid = requset.parameters.password;
log.debug("myPwdGuid", myPwdGuid);
context.response.write(myPwdGuid);
}
}


return {
onRequest: onRequest
};
});

CTA - NetSuite SuiteCommerce Implementation Banner

Create Connection To Your Server

The connection can be created in either the same GUID generator script or in an entirely new script. The sample code for the connection is as follows:


function mainFunc(context) {
var myHostKey = "Your Host-Name”;
var connection = sftp.createConnection({
username: 'root',
passwordGuid: "Guid generates from suitelet”,
url: 'sample url’,
directory: '/',
hostKey: myHostKey,
port: 22
});

Furthermore, the STFP module must be loaded to connect to a remote FTP server via SFTP and transfer files.

CTA - NetSuite Wayfair Connector Banner

Upload And Download File as per your requirement

Finally, the file to upload to the path of the server can be created using the code given below:

var myFileToUpload = file.create({
name: 'file.txt',
fileType: file.Type.PLAINTEXT,
contents: 'This is uploaded using sftp API in SuiteScript 2.0'
});


connection.upload({
directory: '/',
filename: file.txt',
file: myFileToUpload,
replaceExisting: true
});

Files can also be downloaded from the directory of the server to the file cabinet in NetSuite through the following function:


var downloadedFile = connection.download({
directory: '/',
filename: file.txt'
});
downloadedFile.folder = folder id in which you want to place your file in file cabinet;
var fileId = downloadedFile.save();

Hope you found this post useful.

To learn more about how to get the most out of your NetSuite investment, contact our trained NetSuite specialists for a free consultation.

Get In Touch With Our Experts


    Get In Touch With Our Experts

      I have read and agree to the Privacy Policy of Folio3
      I agree to be contacted by Phone or Email by Folio3

      Get in touch with the

      Award-Winning

      End-to-end NetSuite Servicing Agency

      Tell us how may we assist you!