OrderCloud JavaScript SDK
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
-
Error: Cannot find module 'axios' or its corresponding type declarations.
cause The `axios` library, a required peer dependency, is not installed in the project.fixInstall `axios` using your package manager: `npm install axios` or `yarn add axios`. -
TypeError: OrderCloudSDK.Auth.Login is not a function
cause This error typically occurs if `Auth` was not correctly imported or accessed. Common causes include trying to use `OrderCloudSDK.Auth` after a named ESM import like `import { Auth } from 'ordercloud-javascript-sdk';` or attempting to destructure `Auth` from a CommonJS `require` call without first getting the main object.fixFor ESM, ensure you use `import { Auth } from 'ordercloud-javascript-sdk';` and then directly call `Auth.Login(...)`. For CJS, use `const OrderCloudSDK = require('ordercloud-javascript-sdk');` and then call `OrderCloudSDK.Auth.Login(...)`.
Warnings
- breaking Major versions (e.g., v12.0.0) often introduce breaking changes. It is crucial to consult the `MIGRATION_GUIDE.md` for specific instructions when upgrading from older versions.
- gotcha The `axios` library is a peer dependency and is not automatically installed with the SDK. Failure to install `axios` separately will lead to runtime errors when making API calls.
- gotcha The SDK relies exclusively on named exports for its modules. Attempting to use a default import will result in `undefined` or runtime errors.
Install
-
npm install ordercloud-javascript-sdk -
yarn add ordercloud-javascript-sdk -
pnpm add ordercloud-javascript-sdk
Imports
- Products
const Products = require('ordercloud-javascript-sdk').Products;import { Products } from 'ordercloud-javascript-sdk'; - Auth, Tokens
import OrderCloudSDK from 'ordercloud-javascript-sdk'; // Auth is not a default export
import { Auth, Tokens } from 'ordercloud-javascript-sdk'; - OrderCloudSDK (full object)
import OrderCloudSDK from 'ordercloud-javascript-sdk'; // No default export for the entire SDK object
const OrderCloudSDK = require('ordercloud-javascript-sdk');
Quickstart
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();