Salesforce Commerce Cloud B2C OCAPI Web Services Client
This project is a node module for interfacing with SalesForce Commerce Cloud's Open Commerce API (OCAPI).
1. Install as a dependency in the repo you need it: npm install @twec/ocapi
1. In Business Manager go to _Administration > Site Development: Open Commerce API Settings_.
1. Select a type (Data or Shop) and a context.
1. The settings text box should get auto-populated with a JSON object.
1. 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",.
1. Add your Client Id to the client_id property.
1. Remove any endpoints that you don't need access to.
1. Click Save.
Failure to configure these settings properly will result in unauthorized messages from OCAPI.
| 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.
* getConfig
* getClientToken
* getUserToken
* getAuthorizedGotOptions
* fetchFromEndpoint
* fetchFromGlobalDataEndpoint
* 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.
``javascript
const ocapi = require('@twec/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);
}
})();