Open Commerce API
Open Commerce API
OCAPI version identifier format: vYEAR_RELEASE, e.g. v18_7 (year 2018, 7th release)
Possible URL patterns for the OCAPI resources
base_url/version_id/resource_type – access multiple resources
base_url/version_id/resource_type/identifier – access individual resource
base_url/version_id/resource_type/action – execute a specific action for the resource –
e.g. password reset
base_url/version_id/resource_type/identifier/relationship_type – nested resources
base_url/version_id/resource_type/identifier/relationship_type/relationship_type_id
base_url/version_id/resource_type/identifier/relationship_type/action
OCAPI Resources Documentation
This is dedicated for situations where you have only POST and GET methods available, and
you need to use some of the other methods.
It is possible to obtain the data from an OCAPI call result as a JSONP script.
For this the URL parameter "callback" is used. When it is provided to a Shop API request, the
result JSON is wrapped in a function call with the provided name of the function.
Due to JSONP calls being on the client-side, for security reasons it is recommended to use it
only with Shop API calls. The Data API calls usually require credentials, which would be
insecure to be stored on the client. More information about OCAPI client identification is
given in the following slides.
OCAPI HTTP success status codes
Successful execution
Each unsuccessful OCAPI call will also produce a fault document in response, that will give
more details about the fault and the related exception.
More information is found in the following documentation articles:
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/HttpStatusCodesAndFaults.html
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/Exceptions.html
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/OptimisticLocking.html
OCAPI Resources and Localization
Shop API resources are served in one locale with fallback to country and default locale.
Parameter “locale” or Header “Accept-Language” or default locale for the site
Provided in format <language code>-<country code>
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/Localization.html
OCAPI authentication
OCAPI client application identification
Host: account.demandware.com
Authorization: Basic Y2xpZW50X2lkOnBhc3N3b3Jk
Content-Type: application/x-www-form-urlencoded
Body:
grant_type=client_credentials
The Authorization:Basic header contains the client id and password separated by colon in
Base64 encoding (client_id:password, base64 encoded)
OAuth 2.0 - obtaining Business Manager user grant
Required to operate as a BM user
Host: insance-client.demandware.net
Authorization: Basic Qk11c2VyOkJNcGFzc3dvcmQ6Y2xpZW50X3Bhc3N3b3Jk
Content-Type: application/x-www-form-urlencoded
Body:
grant_type=urn:demandware:params:oauth:grant-type:client-id:dwsid:dwsecuretoken
The Authorization:Basic header contains the Business Manager user and password, followed
by Account Manager client password separated by colon in Base64 encoding
(BMuser:BMpassword:clientid_password, base64 encoded)
OCAPI OAuth 2.0 Response
Client credentials grant type tokens expire in 30 minutes (expires_in 1799 seconds)
Business Manager user grant type tokens expire in 15 minutes (expires_in 899 seconds)
In subsequent OCAPI calls, you need to take the value of access_token and construct a
header "Authorization: Bearer -token-" with it.
OCAPI Configuration
Client ID
Client ID is needed for all OCAPI calls and is configured in Account Manager
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/AccountManager/AccountManagerAddAPIClientID.html
Once client ID is configured and enabled in Account Manager, it is needed to do
corresponding configuration about resource-level access that this client id will have to various
OCAPI resources.
Such configuration is also needed for the 30a client id on your sandboxes.
OCAPI Settings
Control OCAPI client id permissions and also the caching of various OCAPI resources.
Done from Administration > Site Development > Open Commerce API Settings
The settings start with JSON document format version identifier, it is the format of the
configuration, it does NOT limit the calls to that specific version.
More information about the configuration is available in:
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/OCAPISettings.html
It is possible to limit the resource properties visible for a particular client:
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/PropertySelection.html
OCAPI Customization
OCAPI hooks
Server-side script functions, typically called before and after the HTTP method execution.
Some are used only to modify the response from the method execution.
For each resource, list of hooks is provided in section named "Customization“.
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/Hooks.html - full list of hooks
Defined in hooks.json, referenced by package.json (used for cartridge initialization)
Special case: validation hooks for adding OCAPI flashes (validation error messages)
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/Flash.html
https://documentation.b2c.commercecloud.salesforce.com/DOC1/topic/com.demandware
.dochelp/OCAPI/19.1/usage/Customization.html for more details.
Custom properties
Complex functionalities may not be possible to easily implement with OCAPI and hooks.
In some cases it may make more sense to implement logic as a custom controller.
Preferred approach is to implement the customization with hooks, whenever possible.