In this tutorial, we will show how easy it is to create a headless shopping cart API using Total.js framework. In the following Chapter 2. we will show how easy you can connect a browser with Total.js WebComponents and in our case we will use the webcomponent j-ShoppingCart from https://componentator.com/components/j-shoppingcart/. To finish our project in chapter 3 we will show you how easy it is to create a progressive web application from our single page application in the second chapter.
To get started we take a moment to explain what actualy will happen in the next chapters.
Creating a headless shoppingcart api without any layout and just data is the way to get started writing modern and independent interfaces. Total.js comes in a way, making it a easy task in everbody's daily work. Following the notation of Total.js, you will get all of the benefits the framework provides to you.
The second chapter will just go in detail how to implement and use the Total.js WebComponent j-ShoppingCart and how you can easly integrate the layout of your choice.
The WebComponent is prepared to be synchronize the data object as a singleton. Including the functionality of synchronizing data between the
LocalStorage and the server.
In the third chapter of this series, we will extend the single page application and deliver it as a progressive web application with Cache and remote network synchronisation functionality.
So have fun and happy coding.
We create a new folder for our project and change into it.
We install the Totaljs v4 framework using the nodejs package manager
We create the necessary folder structure for controllers and schemas:
Optional but helpful is a ./config file. If there is a config file in the root folder of the application, this file is read when the application is started and can influence the application and the behavior at runtime.
Here are some more informations about Total.js framework configurations:
We create the
index.js startup file in the root folder of the project.
Use your favorite editor and edit the
./totaljs-cart-api/index.js with following content.
We create the file
/controllers/api.js in the controllers directory and we use the
ROUTE() function of Total.js:
We recommend this introduction of Louis to learn more about Total.js API routing. Totaljs API Routing
Lets start our server.
You should see the following console output in your terminal.
You will see additional informations as output in your console.
console.log() you see your debugging output right there.
Total.js framework provides a built-in NoSQL system or TextDB that we can use as a database interface. To use this, we add the appropriate functions to our controller or schema.
This type of TextDB stores documents (objects) with the predefined schema. In other words: you can store documents with fields only that are defined in the Table schema. The table is much faster than the NoSQL database, but it's limited due to the declaring of the schema.
In the folder
/schemas we create a the cart schema file named
Total.js Schemas are one of the most used parts of the Total.js framework. Schemas offer you full control over incoming data. We can execute schemas from routes directly as we do in our example. (look to ROUTE() method ).
You can use any REST API client tool like Curl, Postman or SWAGGER.
Let's say we have
/api/cart_insert endpoint for adding products using
Curl POST JSON Syntax:
We can use the command
curl to send the api schema.
Update: Curl is as well included in Windows, no need to run it via PowerShell.
To use it, just open the command prompt and change the directory to the folder you downloaded curl.exe and type in if you copied the executable to
To follow the definition of j-ShoppingCart singleton, we use the following json definition:
1. items: This is an array containing multiple cart item objects. Each item object has the following properties:
2. price: A number representing the total price of all cartitems in the list. This could potentially be the sum of all "total" values from the "items" array.
3. total: A number representing an overall total value. It represents the final price after applying discounts, delivery costs or taxes.
4. count: A number representing the total quantity of all items in the list. Sum of all "count" values from the "items" array.
Define temporary fake-data in your terminal with
as a environment cariable.
If everything works like expacted, you should see the nested environment variables in your terminal console.
Lets try our first api call.
Preparing CRUD testing
Inserting cart items.
Read the API with specific cart item id
cURL UPDATE API Call
cURL DELETE API Call
API Websocket payload:
Look to the difference building the json object to send to the websocket api implementation.
How to test the websocket api implementation:
It can be tricky, but with this cURL command it should be possible get in touch with the Total.js Websocket API Implementation.
Advance the API with a payment provider
How to implement a payment gateway provider like paypal, molly or stripe?
If you are interessted in it, just leave a line at email@example.com and we will get in touch.
With Total.js, we can create a powerful API in a short time. The framework's clear structure and intuitive methods speed up the development process significantly. Inlcuding a realtime websocket based api structure, it is now easy to connect with several clients written in Rust, Go, PHP, C/C++, Ruby and more.