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:

mkdir test-commerce-sdk
cd test-commerce-sdk
npm init -y
npm 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.

const { ShopperSearch, ShopperLogin, slasHelpers } = require("commerce-sdk");

const SLAS_CLIENT_ID = "da422690-7800-41d1-8ee4-3ce983961078";
// WARNING: Secret is provided here for convenience only. Do not include secrets in your code!
const SLAS_CLIENT_SECRET = "D*HHUrgO2%qADp2JTIUi";
// const SLAS_CLIENT_SECRET = process.env.SLAS_CLIENT_SECRET
const ORG_ID = "f_ecom_zzte_053";
const SHORT_CODE = "kv7kzm78";
const SITE_ID = "RefArch";

// Client configuration parameters
const clientConfig = {
  headers: {},
  parameters: {
    clientId: SLAS_CLIENT_ID,
    organizationId: ORG_ID,
    shortCode: SHORT_CODE,
    siteId: SITE_ID,
  },
};

/**
 * Get the shopper or guest JWT/access token, along with a refresh token, using client credentials
 * using the Shopper Login API directly. See below for an example of using the slasHelpers utility function instead.
 * @returns guest user authorization token
 */
async function getGuestUserAuthToken() {
  const credentials = `${SLAS_CLIENT_ID}:${SLAS_CLIENT_SECRET}`;
  const base64data = Buffer.from(credentials).toString("base64");
  const headers = { Authorization: `Basic ${base64data}` };
  const client = new ShopperLogin(clientConfig);

  return await client.getAccessToken({
    headers,
    body: {
      grant_type: "client_credentials",
      channel_id: SITE_ID,
    },
  });
}

// Get a JWT to use with Shopper API clients
async function run() {
  let tokenData;
  try {
    tokenData = await getGuestUserAuthToken();

    // Or use the slasHelpers utility function:
    
    // const client = new ShopperLogin(clientConfig);
    // tokenData = await slasHelpers.loginGuestUserPrivate({
    //   slasClient: client,
    //   credentials: {
    //     clientSecret: SLAS_CLIENT_SECRET,
    //   }
    // })
  } catch (e) {
    console.error(e);
    console.error(await e.response.text());
    return;
  }

  // Add the token to the client configuration
  clientConfig.headers["authorization"] = `Bearer ${tokenData.access_token}`;

  // Search for dresses
  const searchClient = new ShopperSearch(clientConfig);
  const limit = 3;
  const searchResults = await searchClient.productSearch({
    parameters: { q: "dress", limit: limit },
  });

  if (searchResults.total) {
    for (i = 0; i < limit; i++) {
      const result = searchResults.hits[i];
      console.log(`💃 ${result.productId} ${result.productName}`);
    }
  } else {
    console.log("No results for search");
  }
}

run();

Run Your Example

Go back to the terminal and run the demo code:

node get-dresses.js

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

💃 25592581M Floral Dress
💃 25593727M V-Neck Dress
💃 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.