<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anas Firari on Medium]]></title>
        <description><![CDATA[Stories by Anas Firari on Medium]]></description>
        <link>https://medium.com/@firarianas?source=rss-d7348846b8e8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*NnDhbSUdhnpBuxgW</url>
            <title>Stories by Anas Firari on Medium</title>
            <link>https://medium.com/@firarianas?source=rss-d7348846b8e8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 13:19:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@firarianas/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Consuming the Open Commerce API from SalesForce Commerce Cloud]]></title>
            <link>https://medium.com/@firarianas/consuming-the-open-commerce-api-from-salesforce-commerce-cloud-4c31e1b5e4a6?source=rss-d7348846b8e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c31e1b5e4a6</guid>
            <category><![CDATA[salesforce-commerce-cloud]]></category>
            <category><![CDATA[b2c]]></category>
            <dc:creator><![CDATA[Anas Firari]]></dc:creator>
            <pubDate>Wed, 15 May 2024 22:16:21 GMT</pubDate>
            <atom:updated>2024-06-27T16:15:52.407Z</atom:updated>
            <content:encoded><![CDATA[<h3>OCAPI: Why is it there and what can we do with it?</h3><p>OCAPI is one of the APIs that Salesforce Commerce Cloud offers on its platform.</p><p>Another API called Salesforce Commerce API also exists, but we will focus on OCAPI in this article.</p><p>One of its common uses are:</p><ul><li>PWA kit (the architecture that will succeed SFRA) uses it along with SCAPI to feed storefront data into its headless frontend components.</li><li>Managing user roles and privileges.</li><li>Gathering data for analytics.</li></ul><p>There are three categories of OCAPI endpoints:</p><ul><li><strong>Data API: </strong>Contains mostly endpoints to resources that are of interest to merchants (Catalogues, products, inventory etc..)</li><li><strong>Shop API:</strong> Contains endpoints that are associated with the shopper experience (Customer password, basket/cart, orders etc..)</li><li><strong>Meta API:</strong> Has endpoints to retrieve information about the resources available in OCAPI and their attributes</li></ul><p>The goal of this tutorial would be to consume the Open Commerce API and retrieve resources through it on a Spring Boot backend.</p><p>To keep things simple for this tutorial we will present these resources through a single controller.</p><p>These are the steps that we will follow:</p><p><strong>Step 1: Getting the ClientID and client secret from the Account Manager</strong><br><strong>Step 2: Enabling Endpoints from the Business Manager</strong><br><strong>Step 3: Using ClientId and ClientSecret to authenticate and retrieve the access token</strong><br><strong>Step 4: Accessing endpoints from the Open Commerce API</strong></p><h3>Step 1: Getting the ClientID and client secret from the Account Manager</h3><ol><li>Open<strong> </strong><a href="https://account.demandware.com/dw/account/Home#!#%2F"><strong>Account Manager</strong></a><strong>.</strong></li><li>Click <strong>API Client</strong>. (If you cannot find the API Client section, ask your organization administrator for access)</li><li>Click <strong>Add API Client</strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*Ln6fckrZIjHVZrmwdnqsSA.png" /></figure><ol><li>Enter the client’s display name.</li><li>Enter the password twice.</li><li>Locate the organization where you want to add a client, select it, and click <strong>Add</strong>.</li><li>Click <strong>Save</strong>.</li><li>Select <strong>API Client</strong> in the left navigation menu.</li><li>Verify that the new client ID is enabled.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*_FqYACpHuCbVzmqN6-7W5w.png" /></figure><h3>Step 2: Enabling Endpoints from the Business Manager</h3><p>From the Business Manager &gt; Administration &gt; Site Development &gt; Open Commerce API Settings</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9pHZ5u7BZY3AFGE0diLjKw.png" /></figure><p>We can select the <strong>API Type</strong> and the <strong>Context</strong> (scope), then we fill the text area to manage resource access privileges, attribute read/write permissions, or configure client application-specific response headers.</p><p>We can specify the Authorized ClientID’s and their corresponding resources access.</p><pre>{<br>    &quot;_v&quot; : &quot;23.2&quot;,<br>    &quot;clients&quot;:[<br>       {<br>          &quot;client_id&quot;:&quot;YOUR_CLIENT_ID_HERE&quot;,<br>          &quot;resources&quot;:[<br>{<br>                &quot;resource_id&quot;:&quot;/sites&quot;,<br>                &quot;methods&quot;:[<br>                   &quot;get&quot;<br>                ],<br>                &quot;read_attributes&quot;:&quot;()&quot;,<br>                &quot;write_attributes&quot;:&quot;()&quot;<br>             },<br>             {<br>                &quot;resource_id&quot;:&quot;/catalogs&quot;,<br>                &quot;methods&quot;:[<br>                   &quot;get&quot;<br>                ],<br>                &quot;read_attributes&quot;:&quot;()&quot;,<br>                &quot;write_attributes&quot;:&quot;()&quot;<br>             },<br>             {<br>                &quot;resource_id&quot;:&quot;/users/&quot;,<br>                &quot;methods&quot;:[<br>                   &quot;put&quot;,<br>                   &quot;get&quot;,<br>                   &quot;patch&quot;,<br>                   &quot;delete&quot;<br>                ],<br>                &quot;read_attributes&quot;:&quot;()&quot;,<br>                &quot;write_attributes&quot;:&quot;()&quot;<br>             }<br>          ]<br>       }<br>    ]<br> }</pre><h3>Step 3: Using ClientId and ClientSecret to authenticate and retrieve the access token</h3><p>In order to retrieve the access token that will allow us to make the API calls, we need to authenticate using the credentials obtained on step 1.</p><p>We make the request on the following URL: <a href="https://account.demandware.com/dw/oauth2/access_token">https://account.demandware.com/dw/oauth2/access_token</a></p><pre>@FeignClient(name = &quot;OCAPIAuthenticationClient&quot;, url = &quot;https://account.demandware.com&quot;)<br>public interface OCAPIAuthenticationClient {<br>  @RequestMapping(method = RequestMethod.POST, value = &quot;/dw/oauth2/access_token&quot;, consumes = &quot;application/x-www-form-urlencoded&quot;)<br>  AccessObject authenticate(@RequestParam(&quot;client_id&quot;) String clientId,<br>                            @RequestParam(&quot;client_secret&quot;) String clientSecret,<br>                            @RequestParam(&quot;grant_type&quot;) String grantType);<br>}</pre><p>The <strong>grantType </strong>needed for this iauthentication is: client_credentials. After sending the request, we receive the following JSON:</p><pre>{<br>&quot;access_token&quot; : &quot;the access token is normally in here&quot;,<br>&quot;scope&quot; : &quot;mail tenantFilter openId profile roles&quot;,<br>&quot;token_type&quot; : &quot;Bearer&quot;,<br>&quot;expires_in&quot; : &quot;1799&quot; <br>}</pre><p>We use AccessObject that allows easy deserialization of the JSON we received and it is defined as follows :</p><pre>public class AccessObject {<br>  @JsonProperty(&quot;access_token&quot;)<br>  public String accessToken;<br>  @JsonProperty(&quot;scope&quot;)<br>  public String scope;<br>  @JsonProperty(&quot;token_type&quot;)<br>  public String tokenType;<br>  @JsonProperty(&quot;expires_in&quot;)<br>  public String expiresIn;<br>}</pre><p>Now that we got our access token, we can use it in future calls to the Open Commerce API.</p><h3>Step 4: Accessing endpoints from the Open Commerce API</h3><p>Using the access token obtained in the authentication, we can make calls to different endpoints of the OCAPI. The URL pattern is as follows:</p><p>We should first know what URLs to use in order to access the information from OCAPI :</p><p>Base URL:</p><p>https://YOUR_INSTANCE.dx.commercecloud.salesforce.com/s/-/dw</p><p>To access the DATA, META or SHOP API, we add it to the base URL, so we get the following URL</p><p>https://YOUR_INSTANCE.dx.commercecloud.salesforce.com/s/-/dw/data_type</p><p>Then we add the OCAPI version</p><p>https://YOUR_INSTANCE.dx.commercecloud.salesforce.com/s/-/dw/data_type/v21_3</p><p>Then finally we add the End Points that can be found on the following link : <a href="https://api-explorer.commercecloud.salesforce.com/"><strong>OCAPI Documentation</strong></a></p><p>In this example, we will get the sites in our organization through an API call to the DATA API. We use a Feign Client interface with the base URL : &quot;https://YOUR_INSTANCE.commercecloud.salesforce.com/s/-/dw&quot;, then define a GET method to access the GET Sites or GET Catalogs endpoint.</p><pre>@FeignClient(name = &quot;OCAPICallsClient&quot;, url = &quot;https://YOUR_INSTANCE.commercecloud.salesforce.com/s/-/dw&quot;)<br>public interface OCAPICallsClient{<br>  @RequestMapping(method = RequestMethod.GET, value = &quot;/data/v21_3/sites&quot;)<br>  String getSites(@RequestHeader(&quot;Authorization&quot;) String token);<br>  @RequestMapping(method = RequestMethod.GET, value = &quot;/data/v21_3/catalogs&quot;)<br>  String getCatalogs(@RequestHeader(&quot;Authorization&quot;) String token);<br>}</pre><p>Notice that we didn’t put the authentication and api call methods in the same Feign Client Interface because they don’t have the same Base_URL.</p><p>We can access other Endpoints. To get Orders, for example, we need to access the SHOP API, and we can do that as follows :</p><pre>@RequestMapping(method = RequestMethod.GET, value = &quot;/shop/v21_3/orders&quot;)<br>String getOrders(@RequestHeader(&quot;Authorization&quot;) String token);</pre><p>Notice that in both methods, we put the token received through the authorization into the request header, and it should be preceded by the word<strong> Bearer</strong> as follows :</p><p>RequestHeader (Authorization : Bearer &lt;access token&gt;)</p><p>Creating the OCAPIService:</p><pre>public class OCAPIService {<br>  private final OCAPIAuthenticationClient authenticationFeignClient;<br>  private final OCAPICallsClient callsFeignClient;<br>  private String accessToken;<br>  public AccessObject authenticate(String clientId, String clientSecret) {<br>    return authenticationFeignClient.authenticate(clientId, clientSecret, &quot;client_credentials&quot;);<br>  }<br>  public String getOrders() {<br>    return callsFeignClient.getOrders(accessToken);<br>  }<br>  public String getCatalogs() {<br>    return callsFeignClient.getCatalogs(accessToken);<br>  }<br>  public String getSites() {<br>    return callsFeignClient.getSites(accessToken);<br>  }<br>}</pre><p>Now we need a controller to access the endpoints from our localhost:</p><pre>@RequestMapping(&quot;/ocapi&quot;)<br>public class OCAPIController {<br>  private final OCAPIService ocapiService;<br>  @GetMapping(&quot;/orders&quot;)<br>  public String getOrders() {<br>    Authenticate();<br>    return ocapiService.getOrders();<br>  }<br>  @GetMapping(&quot;/sites&quot;)<br>  public String getSites() {<br>    Authenticate();<br>    return ocapiService.getSites();<br>  }<br>  @GetMapping(&quot;/catalogs&quot;)<br>  public String getCatalogs() {<br>    Authenticate();<br>    return ocapiService.getCatalogs();<br>  }<br>  public void Authenticate() {<br>    AccessObject accessObject = ocapiService.authenticate(&quot;YOUR_CLIENT_ID&quot;, &quot;YOUR_CLIENT_PASSWORD&quot;);<br>    ocapiService.setAccessToken(&quot;Bearer &quot; + accessObject.getAccessToken());<br>  }<br>}</pre><p>If we access: <a href="http://localhost:8080/ocapi/sites"><strong>http://localhost:8080/ocapi/sites</strong></a> we can obtain the following result:</p><pre>{<br>  &quot;_v&quot;:&quot;21.3&quot;,<br>  &quot;_type&quot;:&quot;sites&quot;,<br>  &quot;count&quot;:7,<br>  &quot;data&quot;:[<br>        {&quot;_type&quot;:&quot;site&quot;,&quot;_resource_state&quot;:&quot;300a33a6725b54a50fe5d5844bfc2d1997e01441cca4e6ec901ac10aee92068a&quot;,&quot;id&quot;:&quot;RefArch&quot;,&quot;link&quot;:&quot;https://znbf-001.dx.commercecloud.salesforce.com/s/-/dw/data/v21_3/sites/RefArch&quot;},<br>        {&quot;_type&quot;:&quot;site&quot;,&quot;_resource_state&quot;:&quot;22c2f60b22d8d18590921b69385b607321993c9577a884a70e0581ee04eaf357&quot;,&quot;id&quot;:&quot;RefArchGlobal&quot;,&quot;link&quot;:&quot;https://znbf-001.dx.commercecloud.salesforce.com/s/-/dw/data/v21_3/sites/RefArchGlobal&quot;},<br>        {&quot;_type&quot;:&quot;site&quot;,&quot;_resource_state&quot;:&quot;c3cd3ef0a5dab5e6f5900ce07917b2c1899a23784c117dcd649441959af4ab7e&quot;,&quot;id&quot;:&quot;SiteGenesis&quot;,&quot;link&quot;:&quot;https://znbf-001.dx.commercecloud.salesforce.com/s/-/dw/data/v21_3/sites/SiteGenesis&quot;},<br>        {&quot;_type&quot;:&quot;site&quot;,&quot;_resource_state&quot;:&quot;d618ce1d22447cde3e0693aced82cb682de4f7c6d7ffdae2887f7b4eb6ba58f9&quot;,&quot;id&quot;:&quot;SiteGenesisGlobal&quot;,&quot;link&quot;:&quot;https://znbf-001.dx.commercecloud.salesforce.com/s/-/dw/data/v21_3/sites/SiteGenesisGlobal&quot;}],<br>  &quot;start&quot;:0,<br>  &quot;total&quot;:7<br>}</pre><h3>Bonus: Hooks</h3><p>Hooks empower developers with a powerful toolset to inject your own customized code into OCAPI resources, allowing to enhance the server-side behaviour of your online store.</p><p>These are the types of hooks that are available:</p><ul><li>before&lt;HTTP_Method&gt;: Execute custom code before the server performs its processing.</li><li>after&lt;HTTP_Method&gt;: Execute custom code after the server’s processing.</li><li>modify&lt;HTTP_Method&gt;Response: Apply final changes to the response document.</li></ul><p>It can possibly be used to extend response data, call other APIs or filter a response.</p><p>This sounds difficult to grasp without a frame of reference to fall back on. Thankfully Salesforce provided us with a collection of hooks on their repo <a href="https://github.com/SalesforceCommerceCloud/sfcc-hooks-collection/tree/main"><strong>SFCC Hooks Collection</strong></a> and that is what we are going to look at so that we can dissect the concept of hooks.</p><h3>Step 1: Enable OCAPI hooks</h3><p>To enable API hooks follow these steps:</p><ol><li>Navigate to Administration &gt; Global Preferences &gt; Feature Switches.</li><li>Check the “Enable Salesforce Commerce Cloud API hook execution” feature switch.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TZ-nN5MiH8KaYlrvjk4iEQ.png" /></figure><h3>Step 2: Set the OCAPI hooks and their scripts</h3><p>All the hooks you develop in your cartridge should be registered in a hooks.json file which would contain the paths to each of your hook scripts and their corresponding resources.</p><p>That hooks.json file should also be declared in the package.json file by adding this attribute:</p><pre>{<br>  &quot;hooks&quot;: &quot;./cartridge/scripts/hooks.json&quot;<br>}</pre><p>and this what the hooks.json file would look like:</p><pre>{<br>  &quot;hooks&quot;: [<br>    {<br>      &quot;name&quot;: &quot;dw.ocapi.shop.basket.billing_address.beforePUT&quot;,<br>      &quot;script&quot;: &quot;./basket_hook_scripts.js&quot;<br>    },<br>    {<br>      &quot;name&quot;: &quot;dw.ocapi.shop.basket.billing_address.afterPUT&quot;,<br>      &quot;script&quot;: &quot;./basket_hook_scripts.js&quot;<br>    }<br>  ]<br>}</pre><p>The path to the hook is declared in the hooks.json file along with the resource that it will hook into.</p><p>So technically you could put your hooks anywhere you like but by convention they’re mostly under this path ./cartridge/scripts/hooks/*.js.</p><p>We’ll take a look at an example hook from the collection we mentioned earlier:</p><p>the resource that this hook is injected into it is: dw.ocapi.shop.product_search.modifyGETResponse</p><p>So it would be called whenever a product is searched for.</p><pre>exports.modifyGETResponse = function (searchResponse) {<br>    if (searchResponse &amp;&amp; searchResponse.count &gt; 0) {<br>        var hits = searchResponse.hits.toArray();<br>        hits.forEach(function (hit) {<br>            if (hit.represented_product) {<br>                var productApi = require(&#39;*/cartridge/scripts/apis/productExtend&#39;);<br>                hit.c_extend = productApi.createExtendedProduct(hit.represented_product.id);<br>            }<br>        });<br>    }<br>};</pre><p>This simple hook intercepts the the response that you get whenever you search for a product.</p><p>Loops through all the products in the search result and extends them with more attributes using this custom method productApi.createExtendedProduct</p><p>You can look at it in more detail here: <a href="https://github.com/SalesforceCommerceCloud/sfcc-hooks-collection/blob/main/commerce-cloud-code/plugin_hooktacular/cartridge/scripts/apis/productExtend.js"><strong>Create Extended Product Hook Script</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c31e1b5e4a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>