DEV Community

Cover image for Getting Started with OCAPI in SFRA - A Powerful Tool πŸš€
DanieleAurilio
DanieleAurilio

Posted on • Edited on

Getting Started with OCAPI in SFRA - A Powerful Tool πŸš€

What is OCAPI?

OCAPI (Open Commerce API) is an API which allows access to our platform data from another application in a secure way, going to request information from the server and returning a value or an HTTP error in case of error ( resource not found, parameters not found etc. ).

There are 3 types of OCAPI: Shop, Data and Meta. The most used is Shop, it provides access to Commerce Cloud Digital storefront functionality.

You can retrive information about all OCAPI resources from salesforce documentation under β€œOpen Commerce API”.

GET IN ACTION!🀘

Hey! yes i know, it was boring, but now we get in action. If you not have installed Postman or Insomnia go to download one of this.

Ok! You are now ready to try making the first API call and getting your first error!

But don't worry, i'm here for explain to you how avoid it and retrive information what you need.

RETRIVE AUTHORIZATION TOKEN

Yes, your first api call is for retrive authorization token. We need some parameters to pass in header or query.

  • client_id, without it we are not able to access information we need, you can find it in business manager under Administration -> Open Commerce API Settings. (Passed as query).

  • Your username, password and client secret (username and password are the same you use for access to business manager, client secret is provided by CRM) which are coding in Base64 (you can use Base64encode) following this structure your_username:your_password:client_secret.

  • grant_type (passed as query): urn:demandware:params:oauth:grant-type:client-id:dwsid:dwsecuretoken.

  • Content-Type: application/x-www-form-urlencoded.

Finally we have all parameters to pass in header/query and can call endpoint (https:// your_host_sandbox /dw/oauth2/access_token) for retrive authorization token with a POST.

Let’s build our API call!

  • Configure your header:

image

  • Configure query:

image

Remember grant_type value is: urn:demandware:params:oauth:grant-type:client-id:dwsid:dwsecuretoken

TOKEN EXPIERD AFTER 15 MINUTES SO YOU HAVE TO RETRIVE NEW TOKEN.

Note: I’am using insomnia environment variables.

πŸŽ‰ Finally we have our authorization token that we can use from now on to execute any API call.

Don't forget to specify in subsequent API call that Authorization will have Bearer your_token value and don't forget to pass your client_id as a query, this must never be missing!

πŸ“¦ Now let's try to retrieve an order, we just need to make GET to call the resource:

https:// your_sanbox_url /s/Your-Site/dw/Shop/OCAPI Version/orders/{ordernumber}

In header we should set:

  • Authorization Bearer your_token
  • Content-Type application/json

DON’T FORGET to set client_id in query.

You can find other api resource in official salesforce documentation.

Top comments (0)