APIs are the future of building fully functional e-commerce solutions. They allow applications of different sorts to easily communicate with each other. This gives companies the ability to employ a service-oriented architecture where each service is independent and focuses on performing a single function.
One of the most prominent types of e-commerce APIs is shopping cart APIs. These APIs allow you to add robust shopping functionality to any channel, even those that are traditionally non-transactional.
Shopping cart APIs are primarily built on the following HTTP endpoints:
Through these endpoints, there is a wide range of use cases for shopping cart APIs. In this post, we’ll look at a few of the most common uses for these APIs.
Users can add items to their cart using HTTP POST requests. The requests contain a list of items in the body. The endpoint fetches the details for all the items on the list. This includes itemId, price, discount, and the desired quantity. A unique cartId is also created. This Id is necessary for future API calls for the cart.
Here is an example of a request to add two of an item to the cart:
{
"cartId": null,
"userAuthToken": null,
"registeredUser": false,
"items": [
{
"itemId": "1000000038",
"quantity": 2,
"group": [
"5e31a1f9fcc2b500089c10e8"
],
"price": {
"sale": 68,
"base": 85,
"discount": {
"price": 0
},
"currency": "USD"
},
"extra": {}
}
]
}
In a distributed architecture, the product catalog and the shopping cart run as separate processes. This means that the shopping cart must get the product details from the product catalog in order to respond to the user’s request. Shopping cart APIs connect the two services allowing the cart to fetch information from the catalog service.
The cart executes an HTTP GET request and the catalog service returns the product details as a JSON array. The JSON is deserialized so the cart items can be read. The products and their details are then added to the cart.
After items are added to the cart, you can retrieve the cart details using HTTP GET requests. For this, you need to specify either the cartId or the userId in the request path. Here is an example of a request using the userId.
https://{baseurl}/cart?userId=5e990062fcef0314e06b649f
The body of the response will provide a JSON serialization of the cart. Below you can see an example of a retrieve cart response:
{
"_id": "5f13883765bc750008b58293",
"deleted": false,
"registeredUser": false,
"cartId": 1534,
"items": [
{
"price": {
"discount": {
"price": 0,
"discountAmount": 0,
"promosApplied": []
},
"sale": 68,
"base": 85,
"currency": "USD"
},
"discountedQuantity": 0,
"group": [
"5e31a1f9fcc2b500089c10e8"
],
"weightUnit": "lb",
"createdAt": "2020-07-18T22:19:41.035Z",
"updatedAt": "2020-07-18T23:44:19.858Z",
"_id": "5f13889465bc750008b58297",
"itemId": 1000000038,
"quantity": 2,
"sku": "1554213",
"taxCode": "PH060771",
"title": "Glo Pillow Light Gray",
"weight": 2,
"lineItemId": 1,
"totalPrice": {
"currency": "USD",
"amount": 136
},
"id": "5f13889465bc750008b58297"
}
],
"userId": "5f138820708ffb0007e609b0",
"allPromosApplied": [],
"createdAt": "2020-07-18T23:39:35.978Z",
"updatedAt": "2020-07-18T23:44:19.878Z",
"__v": 0,
"totalAmount": {
"currency": "USD",
"amount": 136
},
"quantity": 2,
"errors": {
"inventory": [],
"price": [],
"promo": []
}
}
Users can remove items from the cart using a PATCH or DELETE request. The items are in the body of the request as a JSON array of product IDs. The endpoint takes the array and deletes the items from the cart.
Here is an example of a remove from cart response:
{
"_id": "5f13883765bc750008b58293",
"deleted": false,
"registeredUser": false,
"cartId": 1534,
"items": [],
"userId": "5f138820708ffb0007e609b0",
"allPromosApplied": [],
"createdAt": "2020-07-18T23:39:35.978Z",
"updatedAt": "2020-07-18T23:46:13.408Z",
"__v": 0,
"totalAmount": {
"currency": "",
"amount": 0
},
"quantity": 0,
"errors": {
"inventory": [],
"price": [],
"promo": []
}
}
Developing commerce APIs can be expensive and time-consuming. Fortunately, commerce services like Fabric offer a complete set of headless commerce APIs ready to connect with your existing e-commerce systems right out of the box. With them, you can create great shopping experiences and scale faster.
Tech advocate and writer @ fabric.