DEV Community

Dhanashree Shinde
Dhanashree Shinde

Posted on

๐Ÿš€ Beginnerโ€™s Guide to REST APIs (With Examples)

๐Ÿ”น 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)