What is an API?
API (Application Programming Interface) is a messenger that lets two software systems talk to each other.
👉 One app requests data or action
👉 Another app responds with data or result
Simple Real-Life Example 🍽️
Restaurant analogy:
- You = Client
- Kitchen = Server
- Waiter = API
You don't go into the kitchen.
You tell the waiter what you want.
The waiter brings the food.
That waiter is the API.
Why APIs are used?
APIs help to:
- Share data between systems
- Keep systems secure
- Avoid direct database access
- Reuse functionality
- Build scalable applications
How an API Works (Basic Flow)
- Client sends a request
- API receives the request
- Server processes it
- API sends a response
Client → API → Server → API → Client
API Request Contains
- URL (Endpoint) – where to send request
- Method – what action to perform
- Headers – extra info (auth, format)
- Body – data (optional)
Example:
GET /users/1
API Response Contains
- Status code (success / error)
- Data (usually JSON)
- Message
Example:
{
"id": 1,
"name": "Ashutosh"
}
Common HTTP Methods
| Method | Meaning |
|---|
| GET | Read data |
| POST | Create data |
| PUT | Update entire data |
| PATCH | Update partial data |
| DELETE | Remove data |
HTTP Status Codes (Must Know)
| Code | Meaning |
|---|
| 200 | Success |
| 201 | Created |
| 400 | Bad request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not found |
| 500 | Server error |
Types of APIs
1️⃣ REST API (Most common)
- Uses HTTP
- Stateless
- Uses JSON
- Easy to use
2️⃣ GraphQL
- Client asks for exact data
- Single endpoint
- Flexible but complex
3️⃣ SOAP
- XML based
- Very strict
- Used in old enterprise systems
4️⃣ Internal API
5️⃣ Public API
- Available for everyone
(Google Maps, OpenAI)
REST API Principles (Easy)
- Uses HTTP methods
- Uses clear URLs
- Stateless (no memory)
- Resource-based
Example:
GET /users
POST /users
GET /users/5
API Authentication
Used to protect APIs.
Common types:
- API Key
- Bearer Token (JWT)
- OAuth
- Basic Auth
Example:
Authorization: Bearer <token>
API Security Basics
- Authentication
- Authorization
- Rate limiting
- HTTPS
- Input validation
Most common:
- JSON ✅
Others:
- XML
- Form data
- Postman
- Thunder Client
- Curl
API Versioning
Used when API changes.
Examples:
/api/v1/users
/api/v2/users
API vs Backend
| API | Backend |
|---|
| Interface | Logic + Database |
| Communication layer | Processing layer |
| Sends/receives data | Stores & computes data |
API vs Webhook
| API | Webhook |
|---|
| Client requests | Server pushes |
| Pull-based | Push-based |
| On demand | Event-based |
Real-World API Examples
- Login / Signup
- Payment (Stripe)
- Maps (Google Maps)
- AI (ChatGPT API)
- Weather apps
- Social media feeds
One-Line Summary (Remember This)
API is a bridge that lets different software systems communicate safely and efficiently.