What is an API ? API Types and different HTTP Methods?

23-Dec-2023| 5min Read

In this tutorial, we will learn ‘what is an API’, ‘What are the different types of API's we have’, ‘Different types of HTTP Methods’, and so on.

As you know, Client devices reside with the end user, and whatever software runs on them is called client software. We also learned about the different types of software we have, like system software, application-level software, and utility software.

We know that a server is the powerful computer that takes the requests, processes them, and returns the response to the client.

Let's consider a domain, for example, www.hotels.com (example domain), which may list the hotels and restaurants and also the dishes (consider, for example).

Let's say we opened the hotels.com website/webapp in our browser, and we opened the list of hotels/restaurants page. The client sends the request to the server to get the respective data.

Now, the server should have a mechanism that should understand the request, process it, and, if required, get the data from the database and return the response to the client. This mechanism is called API (in layman’s words).

API means Application Programme Interface; it's an interface to communicate between two software programmes (here, client software and server software).

For example, there are two operations: one is getting a list of hotels from the server, and the other is getting a list of dishes from the server.

If we want to perform the first operation, we need to send a request to the server. Let's say the request is: www.hotels.com/api/list/hotels.

If we want to perform a second operation, we need to send a request to the server. Let's say the request is: www.hotels.com/api/dishes.

Here, www.hotels.com/api/list/hotels and www.hotels.com/api/dishes are called API Endpoints.

As we discussed in our previous tutorial, there are a few technologies that help create server-level applications, for example, Node.js technology. We need to create server-level applications to cater to these endpoints. If these endpoints are not handled by the server, then these endpoints (requests) will not work.

what-is-an-API

Usually there are 4 API Mechanisms we have

  1. SOAP API
  2. REST API
  3. WEB SOCKET API
  4. RPC API

We will not discuss SOAP APIs or RPC APIs in this tutorial.

REST API: In short, there will be a request sent to the server, and the server will cater to that request and reply with a response. Once the response is received by the client, the connection gets closed. It's true; request→response, then connection gets closed.

For example, if we are performing the first operation, i.e., getting a list of hotels, then the request will go to endpoint /api/list/hotels, which will be handled by the server. It will respond with the respective data, and this connection gets closed.

Again, if we want to perform an operation (same or different), then request, response, and connection get closed.

In the REST API, unless the server is queried with the request, it will not respond with the result. This is the trade-off (disadvantage) of the REST API (actually, it's not the disadvantage; rather, that’s how it works).

Let's take an example of a chat application (example: WhatsApp), and if it uses the REST API, if we send a message to our friend, this message will not be delivered to our friend unless the client queries/requests the server. Even if a client queries the server frequently about whether any new message is received (let's say every 5 minutes or so), it is still a problem as the message has not been delivered on the spot (real time).

To solve such problems, we have ‘WEB SOCKETS’.

In web sockets, requests will be sent from client to server and responses will be received from server to client. Once the client receives the response, the connection will not get closed; rather, this connection will live till the client closes the connection, or the server closed the connection, or there is some error.

Let's take four operations

  1. Getting Hotel List
  2. Creating New Hotel
  3. Editing Existing Hotel
  4. Deleting Existing Hotel
what-are-HTTP-methods

As we discussed, every operation will have a unique endpoint to cater to the request. To perform each of these REST operations, we need to perform different ways/methods, which we call as HTTP methods.

To get any data from the server, we need to use the ‘GET’ Http method.

To create any resource on the server, we need to use the ‘POST’ Http method.

To edit any resource on the server, we need to use the ‘PUT/PATCH’ Http method.

To delete any resource on the server, we need to use the ‘DELETE’ Http Method.

Now, our first operation is ‘Getting Hotel List'. For this, we need to send a ‘GET’ request to the server with the respective endpoint (let's say /list/hotels).

If we want to create a hotel, we need to make a ‘POST request to the server with the respective endpoint (let's say ‘create-hotel’).

If we want to edit an existing hotel, we need to send a ‘PUT/POST’ request to the server with the respective endpoint (let's say 'edit-hotel').

Same way, if we want to delete a hotel, then we need to send a ‘DELETE’ request to the server with the respective endpoint (let's say ‘delete-hotel’).

The endpoints that I mentioned are just for example; these endpoints depend on the server application; whatever is set in the backend/server it needs to be used by the client.

We will see practically how we perform all these operations in future tutorials. Please note that, to simplify things, I have mentioned only a few important points. As a beginner, these are enough for now.

Download / Share on:
left iconHow websites works in simple language. How DNS Works.
What is an API ? API Types and different HTTP Methods?right icon