2.5.1 • Published 3 years ago
@fye/ocapi v2.5.1
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 @fye/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
_vproperty 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_idproperty. - 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
getConfiggetClientTokengetUserToken
Generic Internal
getAuthorizedGotOptionsfetchFromEndpointfetchFromGlobalDataEndpoint
Endpoint-specific
searchOrdersgetOrderupdateOrdersearchProducts- see example belowgetProduct- see example belowgetProductOptions
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);
}
})();