Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kheast
Forked from michoelchaikin/postmanNetSuiteTBA.js
Created August 21, 2019 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kheast/afd4f831e25355f0937bcd7f9e2ff85d to your computer and use it in GitHub Desktop.
Save kheast/afd4f831e25355f0937bcd7f9e2ff85d 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_2017_2.platform.webservices.netsuite.com" xmlns:ns1="urn:core_2017_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-SHA1">{{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.HmacSHA1(baseString, key).toString(CryptoJS.enc.Base64);
pm.environment.set("signature", signature);
pm.environment.set("nonce", nonce);
pm.environment.set("timestamp", timestamp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment