Skip to content

SuavecitoInc/netsuite-suitescript-2.x

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NETSUITE

These scripts can be used as examples. NetSuite is highly extendable / customizable scripts may or may not work out of the box. They may depend on custom code / apps / plugins.

SuiteScript 2.x

Contents:

NetSuite Documentation:

Token Based Authentication (TBA)

Setting up Token-Based Authentication for a RESTlet

I. Acquiring the Consumer Key and Consumer Secret

  1. Go to Setup > Integrations > Manage Integrations > New. Fill out the form.
  2. Enable Token-Based Authentication
  3. You will receive the following message, along with the Consumer Key and Consumer Secret. Save it somewhere secure.
Warning: For security reasons, this is the only time that the Consumer Key and Consumer Secret values are displayed.
After you leave this page, they cannot be retrieved from the system. If you lose or forget these credentials, you
will need to reset them to obtain new values.

Treat the values for Consumer Key and Consumer Secret as you would a password. Never share these credentials
with unauthorized individuals and never send them by email.

II. Acquiring the Token ID and Token Secret

  1. If your role is granted with User Access Token permission (If your reading this you should have Admin privileges), you should be able to see Manage Access Tokens inside the settings portlet.
  2. Click on New My Access Token
  3. Select an Application Name and enter your preferred Token Name.
  4. Click Save
  5. You will receive the following message, along with the Token ID and Token Secret. Save it somewhere secure.
Warning: For security reasons, this is the only time that the Token ID and Token Secret values are displayed. After
you leave this page, they cannot be retrieved from the system. If you lose or forget these credentials, you will need
to reset them to obtain new values.

Treat the values for Token ID and Token Secret as you would a password. Never share these credentials with
unauthorized individuals and never send them by email.

III. Testing with Postman

  1. Select the appropriate request method (ex: GET, POST, PUT, DELETE).
  2. Enter the URL you can grab it from the deployment record / integration record.
  3. Click on Authorization.
  4. Select OAuth 1.0
  5. Enter the following Parameters:
    • Consumer Key (from Section I, Step 3)
    • Consumer Secret (from Section I, Step 3)
    • Access Token (from Section II, Step 6)
    • Token Secret (from Section II, Step 6)
  6. Enter the NetSutie Account ID under Advanced > Realm.
  7. Test

General Setup

  1. Create File
  2. Save Files to File Cabinet
    Documents > Files > New
  3. Create SuiteScript Record
    Customization > Scripting > Scripts > New
    Save & Deploy to skip next step
  4. Deploy
    Customization > Scripting > Script Deployments

RESTLet Client Oauth

import fetch from 'node-fetch';
import OAuth from 'oauth-1.0a';
import crypto from 'crypto';
import dotenv from 'dotenv';

dotenv.config();

const config = {
  accountId: process.env.NETSUITE_ACCOUNT_ID,
  consumer: {
    key: process.env.NETSUITE_CONSUMER_KEY,
    secret: process.env.NETSUITE_CONSUMER_SECRET
  },
  token: {
    key: process.env.NETSUITE_TOKEN_KEY,
    secret: process.env.NETSUITE_TOKEN_SECRET
  }
};

async function authenticatedFetch<ResponseType, PayloadType>(
  method: 'GET' | 'POST',
  endpoint: string;
  data: PayloadType
) {
  try {
    const { accountId, consumer, token } = config;

    const oauth = new OAuth({
      consumer,
      signature_method: 'HMAC-SHA256',
      hash_function(baseString, key) {
        return crypto
          .createHmac('sha256', key)
          .update(basestring)
          .digest('base64');
      },
      realm: accountId,
    });

    const authorization = oauth.authorize({
      endpoint,
      method
    },
    token);

    const header = oauth.toHeader(authorization);
    header.Authorization += `, realm="${accountId}"`;

    const response = await fetch(endpoint, {
      method,
      headers: {
        'Content-Type': 'application/json',
        Authorization: header.Authorization,
        'user-agent': 'Some User Agent/v.0.0.1 (Language=JavaScript/Node)'
      }
      body: JSON.stringify(data),
    });

    const responseJson = (await response.json() as ResponseType);

    // do something with response
    return responseJson
  } catch(err: any) {
    console.error('error', err.message);
  }
}

Resources:

Releases

No releases published

Packages

No packages published