Create an E-Commerce Web API from scratch in 14 minutes
Tags: webdev, programming, tutorial, productivity
Published: 2022-10-11 08:50:56

It's easy to dismiss [Hyperlambda](https://aista.com/hyperlambda/) as a toy language when you look at it; No OOP, no complexity, barely any typing, etc. I am therefor on a mission to show you what you can do with it, and how much time you'll save as you do it. In the following video I am therefor creating an E-Commerce Web API from scratch in 14 minutes, replacing the most important parts from Udemy and Coursera, and I'm doing it in 14 minutes. Basically, I am creating an E-Commerce Web API from scratch in 14 minutes, where I can sell access to courses. I start out with _nothing_, creating my database, generating my CRUD API, implementing my business logic, resulting in automatic Stripe integrations for access to my courses.

{% embed https://www.youtube.com/watch?v=9_En5GAjOao %}

Of course, the selling of courses in the above video, can be replaced with selling of any type of intellectual property - However, I wanted a specific use case illustrating an actual _"business need"_ for my video. The way the above thing works, is that it creates two tables in my database.

* courses
* sessions

Courses are accessible by anyone, while sessions are only accessible by users having paid for access. Paying for access, implies invoking [Stripe](https://stripe.com), and add the user to a role when a successful payment goes through - For then to use [Magic's](https://aista.com) internal RBAC (Role Based Access Control) system to prevent users from accessing individual sessions unless they pay.

This allows you to use the content of your _"courses"_ database table to market and sell your courses, while having only paid users able to actually access _"sessions"_ that contains the actual course material. Below is a screenshot of my database model. Of course, my implementation is fairly naive, and only asks users to pay for access to _all_ courses, etc - However, it fully features Stripe integrations, accepting of payments, giving access only to paid users, having a real SQL based database backing your courses, etc. Creating the frontend is an exercise I'll leave to you to figure out.

![The E-Commerce web API data model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa8fu5lwcfptc7fjt9ef.jpg)

For this session I don't create a frontend. I could have done so of course, but you'd probably want a slightly more _"custom"_ frontend than what Magic generates automatically for such cases.

When I am done, I go through how to register at the site, submit a payment method (card information), perform a purchase, for then to show how this gives me access to the course afterwards. I am using the [Hyperlambda Stripe micro service module](https://github.com/polterguy/stripe) as my actual payment module. This lets me get away with a significantly simplified API, since the Magic Stripe module takes care of everything that's _"complex"_ for me automatically, and simply hides it for me.

Notice, if you want to allow users to automatically register in your app, you might benefit from reading about [how registrations works in Magic](https://docs.aista.com/tutorials/registering/), to have users verify their email address, etc. You might also benefit from taking the [Hyperlambda course](https://aista.com/hyperlambda-101/) and check up the [Hyperlambda FAQ section](https://aista.com/hyperlambda-faq/) on our website.

Below is the code I created for my slot in the above video ...

## 98.2% of my code was automagically created

```
slots.create:payments.payments.paid.paid_for_course
   data.connect:magic
      data.create
         table:users_roles
         values
            user:x:@.arguments/*/username
            role:course_client
```

This was the _only_ code I had to manually write. The system contains 355 lines of code in total, still I only had to write 7 lines of code myself. This implies that Magic created 98.2% of my code 100% automagically for this particular use case. This is probably an _"extreme"_ use case - However, even for more complex use cases, Magic typically automatically generates roughly 80% of your code automatically, resulting in that you become 5x more productive.

To configure Stripe and SMTP settings, you'll need something such as follows in your _"Configuration"_ menu item in your cloudlet.

```
    "stripe": {
      "token": "sk_test_sdfougfDGGFHIFDsdougds46664545YOURSECRETKEY"
    },
    "smtp": {
      "host": "smtp.sendgrid.net",
      "port": 465,
      "secure": true,
      "username": "apikey",
      "password": "SG.sdougdfsiughdfiugdfiug4374YOURPASSWORD",
      "from": {
        "name": "Aista Magic Cloud",
        "address": "info@aista.com"
      }
    },
```

You can register a cloudlet and get started below.

* [Aista's CRUD generator](https://aista.com)