# API clients

# Introduction

Using the rocket ignite and commercetools add processes, we have the capability to create API clients. These API clients play a crucial role in interacting with the Commercetools platform.

Additionally, Rocket generates API roots specifically designed for communication with Commercetools. These API roots are directly integrated into the codebase for sending requests.

It's important to make a distinction between API clients and API roots. The former provides the necessary credentials and scope required by the latter to function effectively.

This setup enables a streamlined and secure interaction between your applications and the Commercetools platform.

# API clients

Rocket has made a selection of key API clients so that each client covers its own area of responsibility, ensuring security and efficiency in development work. Each of these selected API clients is tailored to a specific task area and has been carefully configured to ensure that it meets the requirements and best practices for interacting with the Commercetools platform.

# Access scope

These are the selected API clients for starting the development process:

(Makes use of the Mobile & single-page application client template)

view_categories
view_published_products
manage_my_profile
manage_my_orders
manage_my_business_units
manage_my_quote_requests
manage_my_shopping_lists
manage_my_quotes
manage_my_payments
create_anonymous_token
view_categories
view_published_products

(Makes use of the Admin client template)

manage_project

# API roots

When using the Commercetools SDK, API roots are used to communicate with Commercetools. These are used directly in the code to send requests.

Rocket provides a set of API roots that are pre-configured and ready for use in combination with the proxy.
These files will be created doing rocket ignite or rocket commercetools add, if you wish to do so.
All these API roots are standalone files that can be customized with middlewares and so on.

# Selected API roots

These are the selected API roots for starting the development process:

Name Client Proxy Usage
meApiRoot ME true Used for user-specific requests that primarily utilize the "me" endpoint.
readApiRoot READ true Fetches products and categories in the frontend
serverApiRoot READ false Fetches products and categories in the backend
adminApiRoot ADMIN false Should not be used in the frontend because it has many access rights. This is responsible for special requests that a normal user should not perform.

Also have a look at the Proxy Documentation to learn more about the proxy.

# Code examples

The following code examples show how to perform basic commerce tasks with the official Commercetools SDK. For more code examples have a look at Commercetools SDK code examples

# SignUp a user

If a request is sent from the frontend to log in a user, the meApiRoot is used. A new token for password credentials flow will be fetched and attached to the cookie by the proxy.

meApiRoot
  .me()
  .login()
  .post({
    body: {
      email: "{email}",
      password: "{password}",
    },
  })
  .execute();

# Add a product to the cart

If a request is sent from the frontend to add a product to a user's cart, the meApiRoot is used.

meApiRoot
  .carts()
  .withId({ ID: "{cartID}" })
  .post({
    body: {
      version: { version }, // Current version of the cart
      actions: [
        {
          action: "addLineItem",
          productId: "{productId}",
          variantId: { variantId }, // VariantID of the product
          quantity: { quantity }, // Quantity that should be added to the cart
        },
      ],
    },
  })
  .execute();

# Get published product projections

If the product projections should be fetched in the frontend, the readApiRoot is used.

readApiRoot.productProjections().get().execute();

If a page is generated on the server, the serverApiRoot is used, as the request then does not have to run via the proxy.

serverApiRoot.productProjections().get().execute();