A Node.js package for authenticating and calling NetSuite REST Web Services using OAuth 2.0.
npm install netsuite-auth2
- Authenticate with NetSuite using OAuth 2.0 (M2M authentication)
- Cache access tokens for reuse
- Invoke NetSuite REST Web Services easily
const NetSuiteWebServices = require("netsuite-auth2");
const netsuiteClient = new NetSuiteWebServices(
"your_account_id", // NetSuite Account ID
"your_client_id", // OAuth 2.0 Client ID
"your_certificate_id", // Certificate ID
"your_private_key", // Private Key (PEM format)
3600, // Token TTL (in seconds)
"RS256" // Signing Algorithm
);
(async () => {
try {
const response = await netsuiteClient.invokeAPI(
"/record/v1/customer", // API path
"GET", // HTTP method
{ limit: 10 }, // Query parameters
null // Request body (if needed)
);
console.log(response.data);
} catch (err) {
console.error("Error calling NetSuite API:", err);
}
})();
Creates an instance of the NetSuite API client.
new NetSuiteWebServices(accountId, clientId, certificateId, privateKey, ttl, algorithm);
-
accountId
(string): NetSuite account ID (replace_
with-
). -
clientId
(string): OAuth 2.0 Client ID. -
certificateId
(string): Certificate ID used for authentication. -
privateKey
(string): Private key for signing JWT. -
ttl
(number): Token time-to-live (TTL) in seconds. -
algorithm
(string): JWT signing algorithm (e.g.,RS256
).
Invokes a NetSuite API with authentication.
-
path
(string): The NetSuite API endpoint. -
method
(string): HTTP method (GET
,POST
,PUT
, etc.). -
queryParams
(object, optional): Query parameters. -
body
(object, optional): Request body.
-
Promise<Axios.Response>
: The API response.
Returns the base URL for NetSuite REST Web Services.
const url = netsuiteClient.getNetSuiteRestWebServicesHomeUrl();
console.log(url);
- Generate a JWT assertion token using the client ID, certificate ID, and private key.
- Request an OAuth 2.0 access token from NetSuite.
- Store the access token in cache for reuse.
- Use the access token to authenticate API calls.
-
axios
- For making HTTP requests. -
jsonwebtoken
- For generating and signing JWT tokens.
This project is licensed under the ISC License.
Created by Naresh Kollipora.