What is an API?
API(Application Programming interface), is a like an intermediary that allows communication between an application with other application or a database.
When using an application, we see a lot of data, in the form of texts, images or video’s etc. These data have to be transferred from one source( say database) to destination(client). The client requests for the data to the API in the server, which in turn fetches it from database and responds/sends to the client.
Consider an example of Netflix, we all know it streams movies and web series. When a new movie is launched, it is not manually added to the code (hardcoded) and shown on website to the user. Developer does not keep updating the code for each releases, its not the right approach. This has to be done dynamically, and its done by using API. Means the data related to the new movie is added to the database only. A Request to API will automatically get the latest movie details without the need to update the code.
In a similar way when we post something on social media, a request is made to API to save that in database. In short, API is hosted on the server that interacts with the Frontend and the Backend. Any access, update, addition or deletion of data can be performed with the use of an API.
Lets look into how this API requests are made from the Client. REST(REpresentational State Transfer) is one of the way an API is written, we call it RESTful API. In this resources(data) is obtained in HTTP, mostly in JSON format.
API call basically requires 4 components:
HTTP Method- GET(retrieve), POST(create), PUT(update) or DELETE(remove).
Endpoint (URI or path)- The specific URL from which data is fetched or where data is sent.
Request (for POST/PUT) - Contains data sent from frontend to the backend, usually in JSON format, that may need to be added to or updated
in the database.
Response - Data fetched by the API, that needs to be displayed to the user.
In order to make a request to get the data, we use GET method. Mention the URI/ path from where to get the data and after getting data send response back to front end.
A REST API in JavaScript looks like this:
const express = require('express');
const app = express();
To get movies data we use GET method, this is just an example
app.get('https://movies.data.com/', (request, response) => {
response.send();
});
POST method is used to create a new entry in the database
app.use(express.json()); // To parse JSON bodies
app.post('/api/data', (request, response) => {
const data = request.body;
response.json({ message: 'Data received by server successfully' });
});
request.body contains the data we want to send to server. After sending the data, server response message is sent back to the client.
PUT method is used to update the existing data in the database
app.put('/api/items/:id', (request, response) => {
const itemId = request.params.id;
const updatedItem = request.body;
// Find the item to update
const index = data.findIndex(item => item.id === itemId);
// Update the item
data[index] = { ...data[index], ...updatedItem };
response.send(data[index]); //send the updated data to client
});
DELETE method deletes specific element with given ID
app.delete('/users/:id', (request, response) => {
const userId = parseInt(req.params.id);
const userIndex = users.findIndex(user =>user.id === userId);
users.splice(userIndex, 1);//deletes 1 element at UserIndex
response.status(204).send();
});
This was a general idea about what API is and how it is used in real world. Thank you for reading!
Subscribe to my newsletter
Read articles from Soujanya T directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by