1.0.1 • Published 5 months ago
netsuite-auth2 v1.0.1
NetSuite OAuth2 API Client
A Node.js package for authenticating and calling NetSuite REST Web Services using OAuth 2.0.
Installation
npm install netsuite-auth2
Features
- Authenticate with NetSuite using OAuth 2.0 (M2M authentication)
- Cache access tokens for reuse
- Invoke NetSuite REST Web Services easily
Usage
Import the Package
const NetSuiteWebServices = require("netsuite-auth2");
Initialize the Client
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
);
Call a NetSuite API
(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);
}
})();
API Reference
NetSuiteWebServices
Constructor
Creates an instance of the NetSuite API client.
new NetSuiteWebServices(accountId, clientId, certificateId, privateKey, ttl, algorithm);
Parameters:
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
).
invokeAPI(path, method, queryParams, body)
Invokes a NetSuite API with authentication.
Parameters:
path
(string): The NetSuite API endpoint.method
(string): HTTP method (GET
,POST
,PUT
, etc.).queryParams
(object, optional): Query parameters.body
(object, optional): Request body.
Returns:
Promise<Axios.Response>
: The API response.
getNetSuiteRestWebServicesHomeUrl()
Returns the base URL for NetSuite REST Web Services.
const url = netsuiteClient.getNetSuiteRestWebServicesHomeUrl();
console.log(url);
Authentication Flow
- 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.
Dependencies
axios
- For making HTTP requests.jsonwebtoken
- For generating and signing JWT tokens.
License
This project is licensed under the ISC License.
Author
Created by Naresh Kollipora.