#
API clients
#
Introduction
Using the rocket ignite
and commercetools add
processes, we have the capability to create
Additionally, Rocket generates
It's important to make a distinction between
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.
Naming environment variables correctly is crucial to ensure the proper functioning of
Rocket will automatically generate and write these environment variables.
For example, CTP_ME_CLIENT_ID="VALUE"
is the correct naming convention for the meApiRoot
.
Depending on which framework you want to use the
#
Selected API roots
These are the selected
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();