npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@abarreraaponte/netsuite-oauth2-client

v0.3.0

Published

High-performance OAuth 2.0 Client Credentials client for Oracle NetSuite, featuring automatic token generation and caching.

Readme

@abarreraaponte/netsuite-oauth2-client

Legal Disclaimer: NetSuite is a registered trademark of Oracle Corporation. This project is an independent open-source library and is not affiliated with, sponsored by, or endorsed by Oracle Corporation or NetSuite.


High-performance, type-safe, and lightweight OAuth 2.0 Client Credentials client for Oracle NetSuite, built specifically for server-to-server (M2M) integrations.

[!NOTE] This library supports only the OAuth 2.0 Client Credentials flow (using private key JWT assertions). Other flows like Authorization Code or Resource Owner Password Credentials are not supported.


Features

  • ESM-First & Lightweight: Fully optimized for ESM, zero external bloating dependencies, and compatible with modern runtimes (Node.js, serverless, edge).
  • Asymmetric JWT Assertion: Implements robust token assertions using private PEM keys and JSON Web Tokens.
  • Pluggable Token Caching: Simple, standard caching interfaces (TokenStorage) allowing you to plug in Redis, SQL database, Prisma, or in-memory storages.
  • AI Agent Compatible: Clean TypeScript types and docstrings making it perfectly suited for LLM toolchains and autonomous coding agents.

Installation

pnpm add @abarreraaponte/netsuite-oauth2-client
# or
npm install @abarreraaponte/netsuite-oauth2-client

Usage

Configure the client by supplying your NetSuite Account ID, Consumer Key, Consumer Secret, Certificate ID, and Private Key:

import {
  NetSuiteClientCredentialsClient,
  type TokenStorage,
  type TokenData,
} from "@abarreraaponte/netsuite-oauth2-client";

// Implement the TokenStorage interface to cache tokens
class MemoryTokenStorage implements TokenStorage {
  private cache: TokenData | null = null;

  async getToken(): Promise<TokenData | null> {
    return this.cache;
  }

  async saveToken(token: TokenData): Promise<void> {
    this.cache = token;
  }
}

const authClient = new NetSuiteClientCredentialsClient(
  {
    accountId: "123456_SB1",
    consumerKey: "YOUR_CONSUMER_KEY",
    consumerSecret: "YOUR_CONSUMER_SECRET",
    certificateId: "YOUR_CERTIFICATE_ID",
    privateKey: `-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----`,
  },
  new MemoryTokenStorage(),
);

// Automatically returns the cached token, or fetches a new one if expired
const accessToken = await authClient.getCurrentToken();

const response = await fetch(
  "https://123456-sb1.suitetalk.api.netsuite.com/services/rest/record/v1/metadata-catalog",
  {
    headers: {
      Authorization: `Bearer ${accessToken}`,
    },
  },
);

Configuration Options

The NetSuiteClientCredentialsClient constructor accepts a configuration object matching the NetSuiteClientCredentialsConfig interface:

| Option | Type | Required | Description | | ---------------- | -------- | -------- | --------------------------------------------------------------------------------------------------------------------- | | accountId | string | Yes | Your NetSuite Account ID (e.g. 123456 or sandbox 123456_SB1). Normalizes internally. | | consumerKey | string | Yes | The client ID / Integration Consumer Key generated in NetSuite. | | consumerSecret | string | Yes | The client secret / Integration Consumer Secret generated in NetSuite. | | certificateId | string | Yes | The Certificate ID (kid) from the NetSuite Client Credentials Setup mapping. | | privateKey | string | Yes | The PEM-formatted private key corresponding to your certificate. | | algorithm | string | No | JWT signing algorithm. Options: "PS256", "PS384", "PS512", "ES256", "ES384", "ES512". Default: "PS256". |


Generating Keys & Certificates (OpenSSL)

Depending on your security architecture, you can use either RSA-PSS (recommended for RSA) or ECDSA (Elliptic Curve, offering faster signing and smaller keys) signatures.

Generate your private key and self-signed certificate using the corresponding OpenSSL commands:

| Algorithm | Key Type | Private Key Generation | Self-Signed Certificate (Public) | | --------- | --------------- | -------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | | PS256 | RSA (2048-bit) | openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048 | openssl req -new -x509 -key private.pem -out certificate.pem -days 365 | | PS384 | RSA (3072-bit) | openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:3072 | openssl req -new -x509 -key private.pem -out certificate.pem -days 365 | | PS512 | RSA (4096-bit) | openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:4096 | openssl req -new -x509 -key private.pem -out certificate.pem -days 365 | | ES256 | EC (prime256v1) | openssl genpkey -algorithm EC -out private.pem -pkeyopt ec_paramgen_curve:prime256v1 | openssl req -new -x509 -key private.pem -out certificate.pem -days 365 | | ES384 | EC (secp384r1) | openssl genpkey -algorithm EC -out private.pem -pkeyopt ec_paramgen_curve:secp384r1 | openssl req -new -x509 -key private.pem -out certificate.pem -days 365 | | ES512 | EC (secp521r1) | openssl genpkey -algorithm EC -out private.pem -pkeyopt ec_paramgen_curve:secp521r1 | openssl req -new -x509 -key private.pem -out certificate.pem -days 365 |

Setting Up in NetSuite:

  1. Upload the generated certificate.pem to your NetSuite mapping settings under Setup > Integration > OAuth 2.0 Client Credentials Setup.
  2. Copy the resulting Certificate ID mapping.
  3. Load the private.pem content (private key) in your backend environment variables to initialize the client.

Local Sample Script

To test the integration locally without setting up a pnpm workspace:

  1. Copy the example env file at the root:
    cp .env.example .env
  2. Fill in the required NetSuite credentials in .env.
  3. Run the sample script:
    pnpm run sample
    # or
    npm run sample

Caching Implementations

Redis Cache Storage

import { createClient } from "redis";
import type { TokenStorage, TokenData } from "@abarreraaponte/netsuite-oauth2-client";

class RedisTokenStorage implements TokenStorage {
  private client = createClient();
  private key = "netsuite:oauth_token";

  async getToken(): Promise<TokenData | null> {
    const data = await this.client.get(this.key);
    if (!data) return null;
    return JSON.parse(data);
  }

  async saveToken(token: TokenData): Promise<void> {
    const ttlSeconds = Math.max(1, Math.floor((token.expiresAt - Date.now()) / 1000));
    await this.client.setEx(this.key, ttlSeconds, JSON.stringify(token));
  }
}

Prisma SQL Cache Storage

import { PrismaClient } from "@prisma/client";
import type { TokenStorage, TokenData } from "@abarreraaponte/netsuite-oauth2-client";

class PrismaTokenStorage implements TokenStorage {
  private prisma = new PrismaClient();

  async getToken(): Promise<TokenData | null> {
    const record = await this.prisma.netSuiteToken.findFirst({
      orderBy: { expiresAt: "desc" },
    });
    if (!record) return null;
    return {
      accessToken: record.accessToken,
      expiresAt: Number(record.expiresAt),
    };
  }

  async saveToken(token: TokenData): Promise<void> {
    await this.prisma.netSuiteToken.create({
      data: {
        accessToken: token.accessToken,
        expiresAt: token.expiresAt,
      },
    });
  }
}

License

This package is open-source software licensed under the Apache-2.0 License.