@fye/ocapi

2.5.1 • Public • Published

OCAPI for Node.js

This project is a node module for interfacing with SalesForce Commerce Cloud's Open Commerce API (OCAPI).

Install

  1. Install as a dependency in the repo you need it: npm install @fye/ocapi

Business Manager Settings

  1. In Business Manager go to Administration > Site Development: Open Commerce API Settings.
  2. Select a type (Data or Shop) and a context.
  3. The settings text box should get auto-populated with a JSON object.
  4. Update the _v property to use the OCAPI version you'll be using (stick with 19.1 unless there's a reason not to), e.g., "_v":"19.1",.
  5. Add your Client Id to the client_id property.
  6. Remove any endpoints that you don't need access to.
  7. Click Save.

Failure to configure these settings properly will result in unauthorized messages from OCAPI.

Environment Variables

Environment Variable Description Required
CLIENT_ID SFCC OCAPI Client ID, like xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Y
CLIENT_SECRET SFCC OCAPI Client password Y
HOST SFCC instance hostname Y
OCAPI_VERSION SFCC OCAPI Version. Defaults to 'v19_1' N
USER_NAME SFCC Business Manager User name, e.g., "admin" ?
USER_PASS SFCC Business Manager User password ?

The client id and client secrets will be the same across all environments, whereas the user name and password will be specific to your sfcc environment (development, dev01, etc.).

The environment variables are used to create a config object that is passed to most methods to automatically do the authentication.

Methods

Authentication

  • getConfig
  • getClientToken
  • getUserToken

Generic Internal

  • getAuthorizedGotOptions
  • fetchFromEndpoint
  • fetchFromGlobalDataEndpoint

Endpoint-specific

  • searchOrders
  • getOrder
  • updateOrder
  • searchProducts - see example below
  • getProduct - see example below
  • getProductOptions

More methods can be added based on the OCAPI endpoints in the spec.

Examples

const ocapi = require('@fye/ocapi');
const config = ocapi.getConfig();

(async () => {
  try {
    const sampleProduct = await ocapi.getProduct(config, 'fye.000000123456789');
    console.log('Sample Product: ', sampleProduct);

    let index = 1;
    for await (let product of ocapi.searchProducts(config, { count: 50 })) {
      console.log(`Product Search Result #${index}: `, product);
      index++;
    }
  } catch (err) {
    console.error('ERROR with searchProducts', err);
  }
})();

Readme

Keywords

none

Package Sidebar

Install

npm i @fye/ocapi

Weekly Downloads

4

Version

2.5.1

License

ISC

Unpacked Size

28.9 kB

Total Files

4

Last publish

Collaborators

  • drudge
  • fye-engineering
  • timonroe
  • strumwolf