BETA
My accountGet started
Oct 102023
Total.js Platform
6 min read

Headless shopping-cart api tutorial

Total.js is a modern and efficient Node.js framework. In this tutorial, we will show how easy it is to create a headless shopping cart API using Total.js.

Simple Headless Shopping Cart API

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.

Getting Started

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.


1. create project folder

We create a new folder for our project and change into it.

2. install Nodejs framework Totaljs v4

We install the Totaljs v4 framework using the nodejs package manager npm.


3. create folder structure

We create the necessary folder structure for controllers and schemas:

4. Create a config file

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.


(optional)

Here are some more informations about Total.js framework configurations:

5. Create a startup file

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.

6. Create the API controller

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

Totaljs documentation:


Starting the server

Lets start our server.

You should see the following console output in your terminal.

You will see additional informations as output in your console.

With console.log() you see your debugging output right there.

7. NoSQL or TextDB as DB interface

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.


TextDB definition

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.

  • TABLE(name) returns TextDB instance
  • each database needs defined schema in /config file like example below:

Table schema declared in /config file:


8. create cart schema with built-in NoSQL

In the folder /schemas we create a the cart schema file named cart.js:

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 ).


9. Testing the API

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:

  • Use the "POST" request type
  • Enter the full URL http://localhost:8000/api
  • choose request body-type "JSON"
  • Enter the api schema action like cart_insert
  • Enter the data object with a product data singleton in JSON format

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.

First you need to download the cURL executable. For Windows 64bit, download it from here and for Windows 32bit download from here After that, save the curl.exe file on your drive.

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 C:\

Curl POST API Call Example:

To follow the definition of j-ShoppingCart singleton, we use the following json definition:

https://componentator.com/components/j-shoppingcart/

Singleton Description:

1. items: This is an array containing multiple cart item objects. Each item object has the following properties:

  • name: A string representing the item's name.
  • price: A number representing the price of an individual item.
  • total: A number representing the total price of the item.
  • count: A number indicating the number of units of this item.
  • date: A date that might represent the purchase or manufacture date of the item.

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 export 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.


Response object


Preparing CRUD testing

Inserting cart items.


Read the API with specific cart item id {"schema":"cart_read/1234567890"}


cURL UPDATE API Call


cURL DELETE API Call


WebSocket message:

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 info@totaljs.com and we will get in touch.

Conclusion:

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.

Support