Establish an SFTP Connection Using an SSH Key; Create, Update, Load, and Delete a Certificate Record

The following sample establishes a SFTP connection using an SSH key that has already been uploaded to NetSuite. It then creates, updates, loads, and deletes a certificate record to show the full CRUD operation. Replace the server URL with your correct URL.

For the SFTP connection, the public key corresponding to the private key in the certificate must be stored in the .ssh/authorized_keys file on the server.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

          /**
 * @NApiVersion 2.x
 */

require(['N/file', 'N/sftp', 'N/certificateControl'], function(file, sftp, certificateControl) {
    var certPath = 'yyy/certificates'
    var certName = 'apiclient_cert.p12';

    // Establish SFTP connection
    var connection = sftp.createConnection({
        username: 'sftpuser',
        keyId: 'custkeysftp_nft_demo_key',
        url: 'my.sftp.example.com',
        port: 22,
        directory: 'inbound',
        hostKey: 'AAAAB3NzaC1yc2EAAAADAQABAAABAQC4gYD1K4lE9QnuYEgRRQChjrAM1+bTT95e71Xv0oQ60ywVQEiedhRqSMbPiCPPB4pjpBdOmPIQCCkug+3XwAQ6uNj3UM11zoGGmg86tyEJT6qGB0SsrQJzHTb3EG38BSrBO0WEzOWeJ8E8YODT3oAj1Nrf8Ls3JbGObRF+0uwJDIllSrFkYS3kWCV27NhBnaytGe7iLBgrJdNVlitNqkxZfK0NsAYCaJWQjQLtz+GFfN5zTbKKNsDa6s/YW7oAMMOI3Q5GQAqdXtKY728WvxYTjr2FsYS/KM6nbq/csTvZHWLE0z2TQtB2H0IIofvEP/QvXwmgEnCeVPcNgRwdHWQf'
    });
// ------------------------------------------------------------------------

// Create new certificate   
    var certScriptId = '_' + (Math.random().toString(36).substring(2, 10));
    var cert = certificateControl.createCertificate();
    cert.name = 'Test Certificate China API';
    cert.description = 'Test Certificate China created using API';
// custcertificate prefix will be added automatically
    cert.scriptId = certScriptId;
    cert.file = connection.download({ 
        directory: certPath,
        filename: certName
    });
//guid corresponding to the certificate's password';
    var pwd = '022b490ad4334c7e86a8304f937ec68f'; 
    cert.password = pwd;
    cert.save();
/**/certScriptId = 'custcertificate' + certScriptId;
    log.debug('Certificate "' + cert.name + '" successfuly created with id "' + certScriptId + '"!');
// ------------------------------------------------------------------------
// Rename certificate
    cert = certificateControl.loadCertificate({scriptId: certScriptId});
    cert.name = 'Test Certificate China API TEMP';
    cert.save();
// Verify new certificate name
/**/cert = certificateControl.loadCertificate({scriptId: certScriptId});
    log.debug('Certificate successfully renamed to "' + cert.name + '"');
// ------------------------------------------------------------------------
// Delete certificate
    certificateControl.deleteCertificate(certScriptId);
    log.debug('Certificate deleted!');
// ------------------------------------------------------------------------
// Load the deleted certificate
    // attempt to load the deleted certificate - this should error
    try {
        cert = certificateControl.loadCertificate({scriptId: certScriptId});
    } 
    catch (e) {
        log.error(e.message);
    }
}) 

        

General Notices