An API is a set of rules that lets two applications talk to each other. You don't need to know how the other app works internally โ you just call the API and get back data.
๐ฝ๏ธ The Restaurant Analogy
You = Your App | Waiter = API | Kitchen = Server | Menu = API Docs
Real world examples: Swiggy โ restaurant menu API. UPI โ payment gateway API. Google Login โ OAuth API. Weather app โ weather service API. Everything is APIs.
๐ APIs You Use Every Day
Swiggy / Zomato โ Restaurant data, GPS tracking, payment โ all separate APIs
Instagram โ Post upload, like, comment, follow โ all API calls
Google Maps โ Embed maps in any app with just 3 lines of code
ChatGPT / Gemini โ Add AI to your apps via simple API calls
2How APIs Work โ Step by Step
โ๏ธ
The Complete Request โ Response Cycle
What happens in milliseconds when you click anything
Beginner
1Client makes a request: Your app sends an HTTP request to an API endpoint URL
2Request travels: Contains HTTP method, headers (auth tokens), and optionally request body
4Server responds: Returns JSON data with HTTP status code (200 OK, 404 Not Found, 500 Error)
5Client displays: Your app reads the JSON and renders it for the user
COMPLETE API CALL EXAMPLE
// 1. Make a GET request to fetch user data$curl -X GET "https://api.example.com/users/42" \
-H "Authorization: Bearer eyJhbGci..." \
-H "Content-Type: application/json"// 2. Server responds with JSON{
"status": 200,
"data": {
"id": 42,
"name": "Rahul Kumar",
"email": "rahul@example.com",
"city": "Mumbai"
}
}// Response time: 142ms โ
JSON โ The Universal Language of APIs:Almost all modern APIs communicate using JSON โ text organized as key:value pairs.
3REST APIs โ The Most Popular Standard
๐
REST โ Representational State Transfer
~80% of all public APIs are REST APIs
Intermediate
REST is a set of architectural principles for designing APIs. REST APIs use standard HTTP methods and clean URLs to operate on resources.
๐ REST Resources = Files in a Folder
Think of your API like a filing system. /users is a folder. /users/42 is one file. GET=read, POST=create, PUT=update, DELETE=remove.
REST ENDPOINTS โ USERS RESOURCE
GET/api/usersโ Get all usersGET/api/users/42โ Get user with ID 42POST/api/usersโ Create a new userPUT/api/users/42โ Replace user 42 entirelyPATCH/api/users/42โ Update part of user 42DELETE/api/users/42โ Delete user 42// Nested resources:GET/api/users/42/ordersโ All orders by user 42GET/api/users/42/orders/7โ Order 7 by user 42
REST's 6 Principles: Stateless ยท Client-Server ยท Uniform Interface ยท Cacheable ยท Layered System ยท Code on Demand.
GraphQL is a query language for APIs created by Facebook. One single endpoint โ ask for exactly the data you need.
๐ REST API
Simple to understand & implement
Widely supported everywhere
Great HTTP caching built-in
Over-fetching extra unwanted fields
Multiple requests for related data
Versioning can get messy
๐ GraphQL
Get exactly the fields you need
Single request for nested data
Strongly typed schema
Steeper learning curve
Complex caching
Overkill for simple apps
REST vs GRAPHQL โ SAME DATA
// REST: Need 3 separate requests!GET /api/users/42
GET /api/users/42/posts
GET /api/users/42/followers
// GraphQL: ONE request, exactly what you needquery {
user(id: 42) {
name
email
posts { title }
followers { count }
}
}
โ Returns exactly name, email, post titles, follower countโ No extra fields, no extra requests โ
7WebSockets โ Real-Time APIs
โก
WebSockets โ Persistent Two-Way Connection
Used in chat apps, live scores, trading, games
Intermediate
Normal HTTP = letters (send, wait, close). WebSockets = phone call (connection stays open, both sides talk anytime).
๐ด Live WebSocket Simulation
Connected
WEBSOCKET CODE
// Open a WebSocket connectionconst ws = new WebSocket('wss://api.example.com/chat');
// Connection opened
ws.onopen = () => {
ws.send(JSON.stringify({ type: 'join', room: 'general' }));
};
// Receive messages in REAL TIME!
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
console.log('New message:', msg);
};
// Send a message
ws.send(JSON.stringify({ text: 'Hey!', user: 'Rahul' }));
8API Authentication
๐
API Keys, JWT Tokens, OAuth โ Explained
How APIs verify you have permission
Intermediate
Most APIs require you to prove who you are before returning data. 3 main methods: