What is an API in the first place?
An API is an Application Programming Interface. By the name, we can infer that this acts as an interface between two programs.
This can be easily understood by a simple analogy, You are at a pizzeria, you order a pepperoni pizza. The waiter takes an order from you and gives it to the kitchen. Once the pizza is ready, the waiter serves you pizza. Now you have an amazing delight.
Takeaway:
The waiter acts as an API here. He helps you to communicate with the Chef. The fact is that you don't know how the pizza is being prepared.
So, an API helps the programs to communicate with each other without knowing what exactly happens in the other program.
Client - a program that requests for a service
Server - a program that responds with a service requested by the client
Rest API
Rest stands for Representational State Transfer. It is a set of rules that the developers follow when they create their API. Each URL is called a request and the data sent back is called the response
An API request is made up of four parts
- Endpoint
- Methods
- Headers
- Data (or body)
1. Endpoint
The endpoint (or route) is the URL that we use to send a request
fetch("https://type.fit/api/quotes")
2. Methods
Methods mean the type of request we send to the server
There are five possible methods that are used to perform CRUD
operations.
CRUD is nothing but the Create, Read, Update and Delete operations we need to perform.
Let us consider an ice-cream shop. The available flavors of ice-cream are vanilla, butterscotch, chocolate and pistachio
GET method - Read data from the server
The GET method is used to request a specified resource. This corresponds to the read operation. It responds with 200 OK
status code if the resource requested is present. If the data is not present it responds with the 404 Not found
error.
The get method is idempotent
i.e making the same request multiple times will produce the same result unless the content is changed in the server
The customer(client) wants to know the flavors of the ice-cream available in the shop. The waiter(API) responds back with the available flavors such as vanilla, butterscotch, chocolate and pistachio
POST method - creates a new resource
The POST method is used to send new data to the server.
The post method is non-idempotent
i.e, sending information multiple times to the server creates multiple instances in the server
Let's assume, the shop has got new stocks of black current and mango flavors, the shop keeper will update the menu with the new flavors. Now the menu will have new flavors added, This is similar to the POST request
PUT method - updates a resource
The PUT method is used to update the existing resource, if the resource to be updated is not present, it will create a new resource. This method is idempotent
Now, for instance, When the shop has loaded few more stocks of chocolate ice-cream, the shopkeeper has to update the menu with the total count of ice-creams.
PATCH method - updates a resource
This method can be used to partially update the resource. This method is not necessarily be idempotent.
DELETE method - deletes a resource
This method is used to delete a resource from a server. This method is idempotent
Let's say, there are no more vanilla ice-creams in the shop, the shopkeeper sends a delete request to delete the vanilla flavor from the menu.
3. Headers
Headers are used to provide information to both client and server
example,
"Content-Type:application/json"
4. Data(or Body)
The data contains information that we want to send to the server. This can be used only when the request is post, put, patch and delete. This cannot be used in case of get request
And we have come to the end, I hope you have learned something!
Connect with me on Instagram, Twitter and Linkedin
Happy Coding!