Quick Start: B2C Commerce API and SDK

Make your first request to the B2C Commerce API using the Salesforce Commerce SDK.

For working with on-demand sandboxes, see Develop With an On-Demand Sandbox.

Our demonstration uses the Shopper Search API to search for dresses in the catalog of an example storefront.

Create a Test Project 

Start by creating a test project and installing the SDK as a package dependency.

Open a terminal and run the following commands:

1mkdir test-commerce-sdk
2cd test-commerce-sdk
3npm init -y
4npm install commerce-sdk

Demo Code 

Create a file called get-dresses.js and open it in a text editor.

Copy the following demo code, and paste it in the file.

1const { ShopperSearch, ShopperLogin, slasHelpers } = require("commerce-sdk");
2
3const SLAS_CLIENT_ID = "da422690-7800-41d1-8ee4-3ce983961078";
4// WARNING: Secret is provided here for convenience only. Do not include secrets in your code!
5const SLAS_CLIENT_SECRET = "D*HHUrgO2%qADp2JTIUi";
6// const SLAS_CLIENT_SECRET = process.env.SLAS_CLIENT_SECRET
7const ORG_ID = "f_ecom_zzte_053";
8const SHORT_CODE = "kv7kzm78";
9const SITE_ID = "RefArch";
10
11// Client configuration parameters
12const clientConfig = {
13  headers: {},
14  parameters: {
15    clientId: SLAS_CLIENT_ID,
16    organizationId: ORG_ID,
17    shortCode: SHORT_CODE,
18    siteId: SITE_ID,
19  },
20};
21
22/**
23 * Get the shopper or guest JWT/access token, along with a refresh token, using client credentials
24 * using the Shopper Login API directly. See below for an example of using the slasHelpers utility function instead.
25 * @returns guest user authorization token
26 */
27async function getGuestUserAuthToken() {
28  const credentials = `${SLAS_CLIENT_ID}:${SLAS_CLIENT_SECRET}`;
29  const base64data = Buffer.from(credentials).toString("base64");
30  const headers = { Authorization: `Basic ${base64data}` };
31  const client = new ShopperLogin(clientConfig);
32
33  return await client.getAccessToken({
34    headers,
35    body: {
36      grant_type: "client_credentials",
37      channel_id: SITE_ID,
38    },
39  });
40}
41
42// Get a JWT to use with Shopper API clients
43async function run() {
44  let tokenData;
45  try {
46    tokenData = await getGuestUserAuthToken();
47
48    // Or use the slasHelpers utility function:
49    
50    // const client = new ShopperLogin(clientConfig);
51    // tokenData = await slasHelpers.loginGuestUserPrivate({
52    //   slasClient: client,
53    //   credentials: {
54    //     clientSecret: SLAS_CLIENT_SECRET,
55    //   }
56    // })
57  } catch (e) {
58    console.error(e);
59    console.error(await e.response.text());
60    return;
61  }
62
63  // Add the token to the client configuration
64  clientConfig.headers["authorization"] = `Bearer ${tokenData.access_token}`;
65
66  // Search for dresses
67  const searchClient = new ShopperSearch(clientConfig);
68  const limit = 3;
69  const searchResults = await searchClient.productSearch({
70    parameters: { q: "dress", limit: limit },
71  });
72
73  if (searchResults.total) {
74    for (i = 0; i < limit; i++) {
75      const result = searchResults.hits[i];
76      console.log(`💃 ${result.productId} ${result.productName}`);
77    }
78  } else {
79    console.log("No results for search");
80  }
81}
82
83run();

Run Your Example 

Go back to the terminal and run the demo code:

1node get-dresses.js

The output of get-dresses.js looks like this:

1💃 25592581M Floral Dress
2💃 25593727M V-Neck Dress
3💃 25589753M Pack-And-Go Dress

For more sample code, see the examples directory in the Commerce SDK repository.

Next Steps 

In this demo, we’ve already taken care of the following setup tasks for you:

  • Created a demo sandbox with demo data, including example products.
  • Created a demo API client with both a client ID and client secret.
  • Configured SLAS to work with the demo sandbox and demo API client.

To use the API and SDK with your own B2C Commerce instances, you must complete these setup tasks for yourself. These tasks are covered in the following guides: Base URL and Request Formation and Authorization for Shopper APIs.

After you’ve completed the setup tasks, explore the SDK’s README and the API specifications in the API Reference.