DEV Community

Cover image for Create an E-Commerce Web API from scratch in 14 minutes
Thomas Hansen
Thomas Hansen

Posted on • Originally published at aista.com

Create an E-Commerce Web API from scratch in 14 minutes

It's easy to dismiss 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.

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, and add the user to a role when a successful payment goes through - For then to use Magic's 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

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 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, to have users verify their email address, etc. You might also benefit from taking the Hyperlambda course and check up the Hyperlambda FAQ section 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
Enter fullscreen mode Exit fullscreen mode

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"
      }
    },
Enter fullscreen mode Exit fullscreen mode

You can register a cloudlet and get started below.

Latest comments (0)