Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
What is RestFul API?
Next article icon

REST API Introduction

Last Updated : 08 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.

REST APIs use HTTP methods (such as GET, POST, PUT, DELETE) to define actions that can be performed on resources. These methods align with CRUD (Create, Read, Update, Delete) operations, which are used to manipulate resources over the web.

11

A request is sent from the client to the server via a web URL, using one of the HTTP methods. The server then responds with the requested resource, which could be HTML, XML, Image, or JSON, with JSON being the most commonly used format for modern web services.

Key Features of REST APIs

  • Stateless: Each request from a client to a server must contain all the information the server needs to fulfill the request. No session state is stored on the server.
  • Client-Server Architecture: RESTful APIs are based on a client-server model, where the client and server operate independently, allowing scalability.
  • Cacheable: Responses from the server can be explicitly marked as cacheable or non-cacheable to improve performance.
  • Uniform Interface: REST APIs follow a set of conventions and constraints, such as consistent URL paths, standardized HTTP methods, and status codes, to ensure smooth communication.
  • Layered System: REST APIs can be deployed on multiple layers, which helps with scalability and security.

Various HTTP Methods Used in REST API

In HTTP, there are five methods that are commonly used in a REST-based Architecture, i.e., POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively. There are other methods that are less frequently used, like OPTIONS and HEAD.

1. GET Method

The HTTP GET method is used to read (or retrieve) a representation of a resource. In the safe path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).

GET /users/123

This request fetches data for the user with ID 123.

2. POST Method

The POST method is commonly used to create new resources. It is often used to create subordinate resources related to a parent resource. Upon successful creation, the server returns HTTP status 201 (Created) along with a Location header pointing to the newly created resource.

POST /users {    "name": "Anjali",    "email": "[email protected]" }

This request creates a new user with the given data.

NOTE: POST is neither safe nor idempotent.

3. PUT Method

PUT is an HTTP method used to update or create a resource on the server. When using PUT, the entire resource is sent in the request body, and it replaces the current resource at the specified URL. If the resource doesn’t exist, it can create a new one.

PUT /users/123 {    "name": "Anjali",    "email": "[email protected]" }

This request updates the user with ID 123 or creates a new user if one doesn't exist.

4. PATCH Method

PATCH is an HTTP method used to partially update a resource on the server. Unlike PUT, PATCH only requires the fields that need to be updated to be sent in the request body. It modifies specific parts of the resource rather than replacing the entire resource.

PATCH /users/123 {    "email": "[email protected]"  }

This request updates only the email of the user with ID 123, leaving the rest of the user data unchanged.

Key Differences Between PUT & PATCH

Both PATCH and PUT are used to update resources on the server, but they differ in how they handle the update process:

PUTPATCH
Replaces the entire resourceUpdates only specified fields
Must send full dataOnly sends changes
IdempotentNot always idempotent
Example: Updating a user’s entire profileExample: Changing just a user’s email

5. DELETE Method

 It is used to delete a resource identified by a URI. On successful deletion, return HTTP status 200 (OK) along with a response body.

DELETE /users/123

This request deletes the user with ID 123.

Idempotence: An idempotent HTTP method is a HTTP method that can be called many times without different outcomes. It would not matter if the method is called only once, or ten times over. The result should be the same. Again, this only applies to the result, not the resource itself.

Create a Simple REST API using Node.js and Express

Now let's create a REST AP and perform the various HTTP operations.

Step 1: Create the folder

Create the NodeJs project by using the following command:

mkdir node-app cd node-app

Step 2: Install the package.json

npm init -y

Step 3: Install Express

To begin building a REST API in Node.js, you need to install Express. Run the following command in your terminal:

npm install express

Step 4: Create the Server

Here’s a basic example of creating a REST API in Node.js using Express

JavaScript
// Import the Express module const express = require('express'); const app = express(); const port = 3000;  app.use(express.json());  // Define a route for GET requests app.get('/users', (req, res) => {     res.json({ message: 'Returning list of users' }); });  // Define a route for POST requests app.post('/users', (req, res) => {     const newUser = req.body;     res.json({ message: 'User created', user: newUser }); });  // Define a route for PUT requests app.put('/users/:id', (req, res) => {     const userId = req.params.id;     const updatedUser = req.body;     res.json({ message: `User with ID ${userId} updated`, updatedUser }); });  // Define a route for DELETE requests app.delete('/users/:id', (req, res) => {     const userId = req.params.id;     res.json({ message: `User with ID ${userId} deleted` }); });  // Start the server app.listen(port, () => {     console.log(`Server is running on http://localhost:${port}`); }); 


Output: To test the API, open http://localhost:3000 in Postman or another API testing tool.

In this example

  • GET /users: This route fetches the list of users (or mock data in this case).
  • POST /users: This route accepts JSON data from the client to create a new user.
  • PUT /users/:id: This route updates the information of a user based on the user ID provided in the URL.
  • DELETE /users/:id: This route deletes a user with the specified ID.

Applications of REST APIs

REST APIs are widely used across various industries to simplify communication between systems. Some common applications include:

  • Social Media: Integrating third-party platforms like Facebook, Twitter, and Instagram for features like login, sharing, and posting.
  • E-Commerce: Managing products, processing payments, handling orders, and customer management.
  • Geolocation Services: GPS tracking, real-time location updates, and location-based services like finding nearby places.
  • Weather Forecasting: Fetching weather data from external sources to provide real-time weather updates and forecasts.

REST API vs GraphQL

GraphQL is another popular approach for building APIs. Here's how it compares to REST API:

FeatureREST APIGraphQL
FlexibilityFixed endpoints, predefined responsesClients request only the data they need
EfficiencyMay require multiple API calls for related dataSingle request fetches nested/related data
Over-fetchingOften returns extra unused dataNo over-fetching – only requested fields
Under-fetchingSometimes needs additional requests to get all dataGets all needed data in one query
ComplexitySimpler for basic use casesMore flexible but requires learning GraphQL queries

Next Article
What is RestFul API?
author
bansal1232
Improve
Article Tags :
  • Node.js
  • Web-API
  • RESTful

Similar Reads

  • What is REST API in NodeJS?
    NodeJS is an ideal choice for developers who aim to build fast and efficient web applications with RESTful APIs. It is widely adopted in web development due to its non-blocking, event-driven architecture, making it suitable for handling numerous simultaneous requests efficiently. But what makes Node
    7 min read
  • What is RestFul API?
    APIs play an important role in the communication between different software systems. Traditional methods of doing this were often complicated, slow, and hard to grow. RESTful APIs solve these problems by offering a simple, fast, and scalable way for systems to communicate using standard web protocol
    7 min read
  • What is an API Endpoint ?
    The API endpoint is the specific URL where requests are sent to interact with the API. In this article, we will discuss API Endpoint their working and the differences between REST API and GraphQL endpoints. Table of Content What is an API Endpoint?How do API endpoints work?What are some best practic
    7 min read
  • How To Write Good API Documentation?
    API (Application Programming Interface) documentation is important for developers to understand how to interact with your API effectively. Good API documentation can make the difference between an API that is easy to use and one that is frustrating, leading to poor adoption rates. This article will
    4 min read
  • Richardson Maturity Model - RESTful API
    The Richardson Maturity Model (RMM), proposed by Leonard Richardson, is a model used to assess the maturity of a RESTful API based on its implementation levels. It consists of four levels, each representing a stage of maturity in the design and implementation of RESTful principles. Let's delve into
    12 min read
  • Top 10 Python REST API Frameworks in 2025
    In a rapidly changing web development scene, REST APIs have emerged as the underlying functionality that allows for the development of scalable and efficient applications. Python is a simple and versatile language, much helped by a mature ecosystem of frameworks for building REST APIs. The right cho
    10 min read
  • Top Web API Interview Questions and Answers (2024)
    Web APIs, or Web Application Programming Interfaces, are interfaces that allow different software applications to communicate and interact with each other over the Internet. They define a set of rules and protocols that enable one application to request and exchange data or perform actions on anothe
    15+ min read
  • What Makes an API RESTful?
    In web development, APIs help different software systems to interact with each other. They allow applications to request data or services from other programs, making it possible for developers to create complex, integrated systems. One common style for designing APIs is REST (Representational State
    6 min read
  • How To Use an API? The Complete Guide
    APIs (Application Programming Interfaces) are essential tools in modern software development, enabling applications to communicate with each other. Whether you're building a web app, mobile app, or any other software that needs to interact with external services, understanding how to use an API is c
    4 min read
  • How does an API Work ?
    API stands for Application Programming Interface which helps software applications communicate with each other to exchange data, features, and functionality. This exchanged data is in the form of HTML, JSON, XML, and Text. In this article, we will discuss the workings of API. We will discuss the fol
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences