NetSuite API Client
The `netsuite-api-client` package provides a robust interface for interacting with NetSuite's SuiteTalk Web Services, specifically focusing on REST calls and SuiteQL queries. It is currently at stable version `1.0.3` and demonstrates an active release cadence with frequent patch updates addressing dependencies and minor fixes. This library distinguishes itself as a direct fork and merge of the formerly popular `netsuite-rest` and `suiteql` packages, specifically designed to address their limitations. Key differentiators include its ESM-only nature, comprehensive TypeScript typings, and the integration of updated dependencies like `got` to mitigate recent CVEs. It is explicitly maintained and supported, offering a more modern and secure alternative for NetSuite integrations. The package supports promise-based operations for standard REST `request` calls and `query` executions, along with a `queryAll` streaming mechanism for efficiently handling large datasets, which is crucial for enterprise-level data operations. Error handling has also been significantly improved to directly surface NetSuite-specific messages.
Common errors
-
Invalid search query. Detailed unprocessed description follows. Search error occurred: Unknown identifier 'not_existing_field'.
cause Attempting to query a non-existent, misspelled, or inaccessible field in a SuiteQL query.fixVerify the field names in your SuiteQL query against NetSuite's schema and ensure the integration role has appropriate permissions. Use `client.request({ path: "record/v1/metadata-catalog/" })` to inspect available resources and fields. -
TypeError: NetsuiteApiClient is not a constructor
cause This error typically occurs when attempting to import an ESM-only package using CommonJS `require()` syntax in a Node.js environment not configured for ESM.fixEnsure your project is configured for ESM by setting `"type": "module"` in your `package.json` file. Change `const NetsuiteApiClient = require('netsuite-api-client');` to `import { NetsuiteApiClient } from 'netsuite-api-client';`.
Warnings
- breaking The package transitioned to ESM-only starting from version 1.0.0. Existing projects using CommonJS `require()` syntax will break and require migration to ESM `import` statements.
- breaking In v1.0.0, the `result()` method (internally used by `request()`) changed its return structure. The parsed JSON data is now encapsulated within a `data` property of the response object, where previously it might have been directly returned.
- gotcha Starting with v0.1.4, error messages from NetSuite are directly surfaced in the `Error` object's `message` property. This simplifies error handling by removing the need to parse the underlying `got` `HttpError` object for NetSuite-specific details.
Install
-
npm install netsuite-api-client -
yarn add netsuite-api-client -
pnpm add netsuite-api-client
Imports
- NetsuiteApiClient
const NetsuiteApiClient = require('netsuite-api-client');import { NetsuiteApiClient } from 'netsuite-api-client';
Quickstart
import { NetsuiteApiClient } from 'netsuite-api-client';
async function runNetsuiteOperations() {
const client = new NetsuiteApiClient({
consumer_key: process.env.NETSUITE_CONSUMER_KEY ?? '',
consumer_secret_key: process.env.NETSUITE_CONSUMER_SECRET_KEY ?? '',
token: process.env.NETSUITE_TOKEN ?? '',
token_secret: process.env.NETSUITE_TOKEN_SECRET ?? '',
realm: process.env.NETSUITE_REALM ?? '',
base_url: process.env.NETSUITE_BASE_URL ?? ''
});
try {
// Perform a GET request to fetch customer records
const response = await client.request({
path: 'record/v1/customer/',
method: 'GET'
});
console.log('Customer data:', response.data.links);
// Perform a SuiteQL query
const transactions = await client.query("select id from transaction where rownum <= 5");
console.log('Transactions:', transactions);
} catch (err) {
console.error('Netsuite API Error:', err.message);
}
}
runNetsuiteOperations();