OrderCloud JavaScript SDK

12.0.0 · active · verified Sun Apr 19

The OrderCloud JavaScript SDK is a modern client library designed for building robust solutions targeting the OrderCloud eCommerce API. It significantly enhances developer productivity and reduces common errors by providing a full feature parity with the underlying API, comprehensive built-in TypeScript support, and an auto-generated API reference. The SDK operates seamlessly in both browser and Node.js environments and offers an ESM module for bundlers that support tree shaking, enabling leaner project bundles. The current stable version is 12.0.0, with minor releases occurring frequently, indicating an actively maintained project. A key differentiator is its deep integration with the OrderCloud platform, offering dedicated functionalities for authentication, advanced filtering, and impersonation. It explicitly handles `axios` as a peer dependency to prevent potential version conflicts and avoid unnecessary bundle bloat.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to authenticate with the OrderCloud API using a username/password flow, store the access token for subsequent API calls, and handles potential errors during the authentication process.

import { Auth, Tokens } from 'ordercloud-javascript-sdk';

// These should ideally come from environment variables or a secure configuration store.
// Using process.env.VAR_NAME ?? '' for illustration, replace with actual secure methods in production.
const username = process.env.ORDERCLOUD_USERNAME ?? 'YOUR_USERNAME';
const password = process.env.ORDERCLOUD_PASSWORD ?? 'YOUR_PASSWORD';
const clientID = process.env.ORDERCLOUD_CLIENT_ID ?? 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'; // Sign up for free at portal.ordercloud.io/register
const scope = ['FullAccess']; // Array of roles, e.g., ['FullAccess', 'BuyerAdmin', 'MeAdmin']

async function authenticateAndLogToken() {
  try {
    const response = await Auth.Login(username, password, clientID, scope);
    const token = response.access_token;
    Tokens.SetAccessToken(token); // Set the token globally for subsequent API calls
    console.log('Successfully authenticated!');
    console.log('Access Token:', token);

    // Example: After successful login, you can make other API calls
    // import { Products } from 'ordercloud-javascript-sdk';
    // const products = await Products.List();
    // console.log('First 20 products:', products.Items.map(p => p.Name));

  } catch (error: any) {
    console.error('Authentication failed:', error.message);
    if (error.response) {
      console.error('API Error Response:', error.response.data);
    }
  }
}

authenticateAndLogToken();

view raw JSON →