NetSuite integrations
Table of contents: Articles
How to connect to NetSuite’s SuiteTalk REST API using node.js
Connecting to NetSuite’s SuiteTalk REST API can be complicated.
The node.js Javascript code below has been tested and works 100%. It uses token-based authentication (TBA) to connect to the NetSuite REST API.
First, install the necessary node modules:
npm install axios oauth-1.0a crypto-js querystring
Create a new code file with the code listed below. Type in your account ID, consumer key, consumer secret, token ID, token secret and company URL in the ‘NetSuite TBA credentials’ section.
Then run the code with:
node FILENAME.js
It should work!
const axios = require('axios'); const OAuth = require('oauth-1.0a'); const CryptoJS = require('crypto-js'); const querystring = require('querystring'); // NetSuite TBA credentials const accountId = 'YOUR_ACCOUNT_ID'; // Your NetSuite account ID, eg. 123456 or 123456_SB1 const consumerKey = 'YOUR_CONSUMER_KEY'; // Your Consumer Key from NetSuite const consumerSecret = 'YOUR_CONSUMER_SECRET'; // Your Consumer Secret from NetSuite const tokenKey = 'YOUR_TOKEN_KEY'; // Your Token ID from NetSuite const tokenSecret = 'YOUR_TOKEN_SECRET'; // Your Token Secret from NetSuite const restApiUrl = 'YOUR_REST_API_URL'; // The REST API URL eg. https://123456.suitetalk.api.netsuite.com or https://123456-sb1.suitetalk.api.netsuite.com // Set up OAuth const oauth = OAuth({ consumer: { key: consumerKey, secret: consumerSecret }, signature_method: 'HMAC-SHA256', realm: accountId, hash_function(base_string, key) { return CryptoJS.HmacSHA256(base_string, key).toString(CryptoJS.enc.Base64); }, }); const getNetSuiteData = async () => { // Base URL for this API call const baseUrl = `${restApiUrl}/services/rest/record/v1/customer`; const queryParams = { limit: 10, offset: 0 }; // Proper encoding of special characters const encodedQueryString = querystring.stringify(queryParams) .replace(/\!/g, "%21") .replace(/\'/g, "%27") .replace(/\(/g, "%28") .replace(/\)/g, "%29") .replace(/\*/g, "%2A"); // Full URL with properly encoded query string const fullUrl = `${baseUrl}?${encodedQueryString}`; const request_data = { url: fullUrl, method: 'GET', }; // Generate OAuth signature const token = { key: tokenKey, secret: tokenSecret, }; const headers = oauth.toHeader(oauth.authorize(request_data, token)); headers['Content-Type'] = 'application/json'; try { const response = await axios({ url: request_data.url, method: request_data.method, headers: headers, }); console.log('Response data:', response.data); } catch (error) { if (error.response) { console.error('Error response:', error.response.data); } else { console.error('Error message:', error.message); } } }; getNetSuiteData();