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, reduce development time and cost.
Ok, 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
These 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.
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 this article to grasp the foundational concepts. In a later article, we will be building a RESTful API using ExpressJs and MongoDB.
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✌️