Thinc. First Code Backend (Part I: Basic RESTful API)

Natchapol Srisang
Thinc.
Published in
4 min readSep 28, 2018

--

In the present, websites and applications are essential to our daily lives, but how are those websites and apps created? If you don’t know, that’s perfect! We gonna talk about that in this blog.

Overview

Most of websites and apps contain two parts, Client-side (which is user interface) and Server-side (which controls scripts and data), or called Frontend and Backend respectively. As shown in the picture.

Overview of webs and apps architecture

Now it’s time to talk about server-side. It consists of Database and API. We might familiar with database, it is a place to keep the data (easy =v=), but what is API?

And that is what we gonna learn in this blog.

What is API?

API stands for Application Programming Interface, it is an intermediate layer between client-side and database to avoid direct connection to the database.

Imagine a web or app as a restaurant. What if there are only customers and cooks without waiters? How can customers order the food? One way to do that is walking to the kitchen to tell the cooks directly, but that sounds weird (really weird) because this can cause many problems, for example, the customers may steal some equipments from the kitchen. This can be messy!

However, if there are waiters, the problems are solved. Customers just order the food to the waiters and they will bring the order to the kitchen for you. Customers don’t need to know how the waiters tell the cooks, but they know that they will get the food.

In this scenario, customers are client-sides that want to connect to the kitchen, which is similar to database, by ordering to the waiters, which are APIs. You can see what will happen if client-side is directly connected to the database, right?

Note that APIs have many types, but the one we gonna talk about is called RESTful APIs (Don’t be sad if you don’t know what it is, you just need to know what it’s called).

Implementation

We just learn that APIs act as an interface for client-side to connect to the database, but how exactly do we implement them? Well, that’s surprisingly easy, because APIs are actually functions.

APIs implementation (just normal functions…)

In the picture, we have four functions. All we need to do is implementing these functions, and now we are ready to use these APIs.

One more thing to do is to tell the client-side how to use them. In order to access these APIs, we need to tell them the routes or endpoints which usually look like URLs, as shown in the picture below.

Routes or endpoints of APIs.

Now the APIS are completed. You can call these APIs by accessing the correspond routes. For example, if you want to use getUser function, you can access it via /api/user/get/:id where id is ID of the user you want. Some routes may require additional data, for instance, when you want to create new user, at least you need to send username, password, and other information of that user. In this case, you may send them with user.json file when calling /api/user/create. Note that the data required are implied in the parameters of functions.

Conclusion

Hopefully at this point, you have the concept of basic websites and applications architecture, especially server-side development, and how to implement APIs. In the next part, we gonna take you to the tour of backend development with a simple tutorial.

For further detail in Thai, please read from this article.

Finally, I hope everyone loves this blog, if you have any questions with regard to this topic, feel free to ask me in Facebook or in the comment below.

Keep calm and stay coding!

Coming next…

--

--