🔹 Introduction
If you are learning web development, you’ve probably heard the term API many times. But what exactly is a REST API, and how does it work?
In this guide, I’ll explain REST APIs in a simple and beginner-friendly way with real examples
=====================================================
🔹 What is an API?
API stands for Application Programming Interface.
It allows two applications to communicate with each other.
👉 Example:
When you use a weather app, it fetches data from a server using an API.
=====================================================
🔹 What is a REST API?
REST (Representational State Transfer) is a way to design APIs using standard HTTP methods.
It is widely used in modern web development because it is simple, scalable, and efficient.
=====================================================
🔹 Common HTTP Methods
Method Use GET Fetch data POST Send data PUT Update data DELETE Remove data
=====================================================
🔹 Simple Example (JavaScript)
🔹 1. GET (Fetch Data)
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(data => console.log(data));
👉 Use: Get data from server
👉 Example: Fetch all posts
🔹 2. POST (Send Data)
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: 'New Post',
body: 'This is a new post',
userId: 1
})
})
.then(response => response.json())
.then(data => console.log(data));
👉 Use: Create new data
👉 Example: Add a new post
🔹 3. PUT (Update Data)
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: 1,
title: 'Updated Title',
body: 'Updated content',
userId: 1
})
})
.then(response => response.json())
.then(data => console.log(data));
👉 Use: Update existing data (replace full resource)
🔹 4. DELETE (Remove Data)
fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'DELETE'
})
.then(() => console.log('Post deleted'));
👉 Use: Delete data
👉 Example: Remove a post
🔹 Real-Life Example
Imagine a food delivery app:
GET → View menu
POST → Place order
DELETE → Cancel order
=====================================================
🔹 Why REST APIs are Important
Used in almost all modern applications
Connect frontend and backend systems
Essential skill for every developer
=====================================================
Top comments (0)