@twec/ocapi
v2.2.0
Published
Salesforce Commerce Cloud B2C OCAPI Web Services Client
Downloads
6
Keywords
Readme
OCAPI for Node.js
This project is a node module for interfacing with SalesForce Commerce Cloud's Open Commerce API (OCAPI).
Install
- Install as a dependency in the repo you need it:
npm install @twec/ocapi
Business Manager Settings
- In Business Manager go to Administration > Site Development: Open Commerce API Settings.
- Select a type (Data or Shop) and a context.
- The settings text box should get auto-populated with a JSON object.
- 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",
. - Add your Client Id to the
client_id
property. - Remove any endpoints that you don't need access to.
- 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 belowgetProduct
- see example belowgetProductOptions
More methods can be added based on the OCAPI endpoints in the spec.
Examples
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);
}
})();