β Introduction to REST API
What is REST API?
A REST API (Representational State Transfer Application Programming Interface) is a set of rules that allows different software applications to communicate with each other over the internet using the HTTP protocol.
When you use an app on your phone or open a website in your browser, much of the data shown to you is fetched from a remote server through a REST API.
π¨βπ» Why Do We Use REST APIs?
When you visit a website, you interact with visual elements like buttons, forms, or images β this is called the User Interface (UI).
Similarly, REST APIs provide a way for applications to interact with each other, just like humans interact with a UI.
For example:
-
A mobile app fetches user data from a cloud server
-
A web app stores form data into a database
-
An app shows weather updates by fetching data from a weather API
π How REST API Works
Letβs break down how REST API works in simple steps:
-
Client (mobile app, browser, etc.) sends a request to a specific API endpoint
-
The server receives the request and processes it
-
The server responds with data, usually in JSON format
-
The client displays the data to the user
Example:
GET https://api.example.com/users
Response:
[ { “name”: “Vishal”, “email”: “[email protected]” } ]
π HTTP Methods Used in REST APIs
Method | Description | Example Use |
---|---|---|
GET | Fetch data | /users β Get all users |
POST | Create new data | /users β Add a user |
PUT | Update existing data completely | /users/1 β Update user 1 |
PATCH | Update part of the data | /users/1 β Update name |
DELETE | Delete existing data | /users/1 β Delete user 1 |
π§ Real-Life Analogy of REST API
Imagine ordering food at a restaurant:
-
You = Client
-
Waiter = REST API
-
Kitchen = Server
-
Menu = API Documentation
-
Order = Request
-
Food = Response
You tell the waiter what you want β waiter goes to the kitchen β brings back your food.
π Benefits of REST API
-
Platform-independent (works with web, mobile, desktop)
-
Lightweight (uses HTTP & JSON)
-
Scalable and modular
-
Easily consumed by frontend and backend developers
π What is REST API in Login & Social Authentication
REST APIs play a vital role in login systems like Facebook Login or Google Sign-In.
Hereβs how the OAuth login flow using REST API works:
-
User clicks “Log in with Google”
-
App redirects to Google login
-
User enters credentials and grants permission
-
Google sends an authorization code back to the app
-
App sends this code to the backend
-
Backend sends a REST API request to Googleβs API to get an access token
-
Google sends back an access token
-
Backend uses the token to fetch user data via another REST API call
π REST API Request & Response Lifecycle
[ Client (App/Browser) ] | Sends Request β [ Server (API Endpoint) ] | Processes Request β Sends Response | [ Client Receives & Displays Data ]