Broadleaf Microservices

Make an Add-to-Cart Request to the API

Table of Contents

This tutorial will show you how to make a very simple add-to-cart API request to your provisioned demo instance of Broadleaf Microservices.

Tip
If you haven’t already, we recommend reading our introductory OpenAPI UI article before going through this.
Tip

While we believe the OpenAPI UI is the best way to experience the Broadleaf Microservices API, if you want to follow this tutorial using the popular Postman utility, please see our Postman Reference article before proceeding.

Goals

  • Using the OpenAPI UI, learn how to make a request to the Cart Operations microservice to add a product to a cart.

Step by Step Guide

  1. Navigate to the add-to-cart operation in the interactive OpenAPI UI.

    1. Visit the OpenAPI UI (https://[your-demo-identifier].admin.blcdemo.com/api/docs).

    2. Find "Cart Operations Service" in the left navigation menu.

    3. Scroll down to the "Cart Items" tag.

    4. Find the POST /cart/items operation. This is the operation we will be making requests to.

      Add to Cart operation
  2. Since this operation is publicly available and not secured, we don’t have to do anything for authentication/authorization. Full steam ahead!

  3. Make a request to add the "Green Ghost" product to a cart.

    1. As documented in the operation’s request body schema, we will need to supply the ID and SKU of the product which we want to add to the cart. This means we need to get the "Green Ghost" product’s ID and SKU. To make this easy, we’ll visit the admin application and get them from there.

      1. Sign into the Broadleaf Admin (https://[your-demo-identifier].admin.blcdemo.com) using the admin user credentials given to you upon signup.

      2. In the navigation menu, select "Products" under "Catalog". This should reveal a grid of products.

        Note
        Wondering where the product data came from? Your demo instance will have some simple seed data already populated at the time it is provisioned. These products come from that seed data.
        1. Click on the "Green Ghost" product.

        2. From your browser’s address bar, grab the value after /products/, which is the ID of the product. In this case, it should be product1.

        3. Copy the value from the SKU field. In this case, it should be HS-GG-20.

    2. Build and send the request.

      1. Return to the add-to-cart operation in the OpenAPI UI, and click the "Try it out" button near the top of the operation to enable drafting the request.

      2. Specify request parameters.

        1. The operation exposes quite a few parameters, but they’re largely optional. To keep things simple, we’ll just be customizing one parameter and leaving the rest as-is:

          1. X-Context-Request

            X-Context-Request is a header containing crucial context information the server will use to "narrow" results. By default, the value for this is loaded from the "Application Context" example. We can’t just directly use this value, though, as we need to make sure it matches the context of your specific provisioned demo instance.

            Change the applicationId property in the value to PD_[your-demo-identifier], where [your-demo-identifier] the unique identifier of your demo (ex: demo123abc). This is the applicationId of the application that was created when you provisioned your demo.

            You can leave tenantId as-is, since your application will have been provisioned within that default tenant.

        2. The UI should now look something like this:

          Filled Add to cart parameters
      3. Specify the request body.

        1. Take another look at the request body schema, named AddItemRequest.

          Tip
          You may need to exit "Try it out" mode to reveal the "Schema" button again. To do this, simply scroll up to the place where the "Try it out" button was, and click the "Cancel" button.

          A lot of these fields are optional or situational, and in the interest of simplicity, we will only supply those that are needed.

          1. currency and locale are needed for pricing and localization purposes. We’ll use "USD" and "en-US".

          2. productId and sku are needed to identify the product. Supply the values obtained earlier from the "Green Ghost" product.

          3. quantity will just be 1

          4. applicationId and tenantId should match the values set in the X-Context-Request header

        2. The UI should now look something like this:

          Filled Add to cart request body
      4. Click the "Execute" button to submit the request.

        Tip
        Before submitting the request, open your browser’s developer tools to the "Network" tab. This will allow you to see the exact request that is made, which can be helpful in understanding how all of the headers (such as X-Context-Request and Authorization) and query string parameters were supplied. Some browsers even support "Copy as cURL" on requests, producing cURL commands that can be executed through a CLI or imported into software like Postman.
  4. Examine the response.

    1. If you scroll down a little, the "Server Response" will be shown. It should be a 200 with your cart and its items.

      Note
      The operation will automatically create a cart to add the product to if not already present, which is why our request succeeded.
      Add to cart response
    2. You’re probably wondering what the data in the payload actually means. To find out, you’ll need to see the response schema next to the 200 entry in the "Responses" section, as shown in the OpenAPI UI article.

      Add to cart response schema

      Voila! This gives you helpful conceptual information about the various fields you’d expect in the response and what they mean.

This marks the end of this tutorial. Nice work!