Skip to content

Instantly share code, notes, and snippets.

@lucasgbd
Forked from michoelchaikin/postmanNetSuiteTBA.js
Last active October 25, 2023 15:53
Show Gist options
  • Save lucasgbd/d9028445ebca89231a97c9e33e8c1f52 to your computer and use it in GitHub Desktop.
Save lucasgbd/d9028445ebca89231a97c9e33e8c1f52 to your computer and use it in GitHub Desktop.
Postman pre-request script to generate TBA variables for NetSuite Web Services
/*
Usage:
1. Define a NetSuite environment in Postman (https://www.getpostman.com/docs/postman/environments_and_globals/manage_environments)
with the following keys set: account, consumerKey, consumerSecret, tokenId, tokenSecret
2. Add this script to your "Pre Request Script" in Postman (https://www.getpostman.com/docs/postman/scripts/pre_request_scripts)
3. Add the Token Passport in your request with variable placeholders
<tokenPassport xmlns="urn: messages_2021_2.platform.webservices.netsuite.com" xmlns:ns1="urn:core_2021_2.platform.webservices.netsuite.com">
<ns1:account>{{account}}</ns1:account>
<ns1:consumerKey>{{consumerKey}}</ns1:consumerKey>
<ns1:token>{{tokenId}}</ns1:token>
<ns1:nonce>{{nonce}}</ns1:nonce>
<ns1:timestamp>{{timestamp}}</ns1:timestamp>
<ns1:signature algorithm="HMAC-SHA256">{{signature}}</ns1:signature>
</tokenPassport>
*/
let account = pm.environment.get("account");
let consumerKey = pm.environment.get("consumerKey");
let consumerSecret = pm.environment.get("consumerSecret");
let tokenId = pm.environment.get("tokenId");
let tokenSecret = pm.environment.get("tokenSecret");
let timestamp = new Date().getTime().toString().substring(0, 10);
let nonce = CryptoJS.lib.WordArray.random(10).toString();
let baseString = `${account}&${consumerKey}&${tokenId}&${nonce}&${timestamp}`;
let key = `${consumerSecret}&${tokenSecret}`;
let signature = CryptoJS.HmacSHA256(baseString, key).toString(CryptoJS.enc.Base64);
let webservices_url = `https://${account.replace('_','-')}.suitetalk.api.netsuite.com/services/NetSuitePort_2021_2`;
pm.environment.set("signature", signature);
pm.environment.set("nonce", nonce);
pm.environment.set("timestamp", timestamp);
pm.environment.set("webservices_url", webservices_url);
@lucasgbd
Copy link
Author

lucasgbd commented Aug 2, 2023

Updating it to version 2021_2 and SHA256.
Also adding the webservices_url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment