paint-brush
A 4-min Introduction To Restful APIsby@azamahjr
129 reads

A 4-min Introduction To Restful APIs

by Azamah JrNovember 2nd, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

API stands for Application programming interface, it is an intermediary between two or more applications or services that provide a set of rules and definitions that define how the applications can connect and communicate with each other. This communication entails sharing data and business functionalities between applications via. HTTP (Hypertext Transfer Protocol) API is an API that conforms to the 6 guiding constraints of REST, such as code on demand. API is built and used to connect and provide communication between our client-facing front end to the back end server and database.

Company Mentioned

Mention Thumbnail
featured image - A 4-min Introduction To Restful APIs
Azamah Jr HackerNoon profile picture

APIs, What Are They?

API stands for Application programming interface. It is an intermediary between two or more applications or services that provide a set of rules and definitions that define how the applications or services can connect and communicate with each other, all while abstracting how each application, involved in the communication is implemented.


This communication entails sharing data and business functionalities between applications via HTTP (Hypertext Transfer Protocol). Web applications use APIs to connect client-facing, frontends to the backends functionality and data.

Why Use APIs?

APIs make it easier for apps running in different environments or written in different languages to share information. This can simplify app development and also reduce development time and cost.


Let's say an application you are developing requires an authentication and authorization feature; Rather than delegating time and resources in building and maintaining a login system, the functionality provided by APIs of services such as Auth0 can be leveraged to authenticate/authorize and login users into the application.

So What Are RESTful APIs?🤔

Well, REST stands for Representational State Transfer Architectural Style. A RESTful API is an API that conforms to the 6 guiding constraints of REST. Such APIs could also be referred to as simply REST APIs


The 6 constraints of REST are



Client-server Architecture

REST architecture is composed of clients, servers, and resources, and it handles requests through HTTP messages, but the clients and servers must be completely independent of each other. The only information the client knows is the URI (Uniform Resource Identifier) of the requested resource. The server provides this requested resource by sending it through an HTTP response. The server shouldn't be able to modify the client application other than providing a requested resource.


Statelessness

Session Data is sent to the server from the client in such a way that each data packet can be understood in isolation. The server does not retain session state or data, instead, session states or data are held with the client.


Cacheability

Clients can cache server responses. Caching can eliminate the need for some client-server interactions which further improves performance


Layered system

Clients and servers may not necessarily be connected directly with each other. They may be mediated by additional layers such as security layers, load balancers, and intermediate servers. These layers could offer additional features like load balancing, shared caches, and security.


Code on demand (optional)

Servers can extend the functionality of a client by transferring executable code.


Uniform Interface

All API requests for the same resource should look the same. The REST API should ensure that the same piece of data belongs to only one URI. Resources shouldn’t be too large but should contain every piece of information that the client might need. If an API violates any of the guiding constraints, it can not be considered a RESTful API.


How RESTful APIs work👩‍🏭

Let's take an example. We want to build a simple application that connects to a server and provides the functionality of creating, reading, updating, and deleting resources (also known as CRUD) within the database.


A RESTful API is built and used to connect and provide communication between our client-facing front end to the back end server and database.

How RESTful APIs work

  • When users of our application make client requests on the front end to retrieve a resource from within the database, the RESTful API is called, it makes a GET request to retrieve the resource,

  • To create a resource within the database the RESTful API makes a POST request.

  • To update a resource the RESTful API makes a PATCH request

  • To delete a resource from within the database, the RESTful API makes a DELETE request to the Server


The resource can be delivered to the client in a variety of formats including HTML, plain text, or Javascript object notation (JSON).


All HTTP methods can be used in RESTful API calls. We've seen the GET, POST, PATCH, and DELETE methods above. To learn more or have a refresher on HTTP, HTTP methods, and status codes as well as the Request-Response cycle check out this article.

Final thoughts

Understanding the concepts of HTTP is fundamental to building and working with RESTful APIs.


If you haven't come across this before, I suggest you take some time to read the article above to grasp the foundational concepts.


I really hope that you enjoyed reading this article and learned something from it. If you have any questions let me know in the comments. I am passionate about sharing knowledge and helping others reach their goals, let's connect on Twitter. That's all for now✌️


First Published here