What is an API?
- API stands for Application Programming Interface.
- An API is a set of rules that allow different software applications to communicate with each other. Think of it like a bridge that connects two systems and lets them share data or services.
Example
- Imagine you are at a restaurant. The waiter (API) takes your order (request), brings it to the chef (server), and then brings the food back to you (response).
Why Do We Need APIs?
- APIs help developers create software programs more easily. Instead of writing complex code from scratch, they can call APIs that already provide the functions they need.
- APIs are also crucial in building modern websites, where heavy data transfers happen between the client and the server.
How Do APIs Work?
APIs work in a simple step-by-step process
- Request: A client sends a request through the API URI.
- Processing: The API forwards the request to the server.
- Response: The server processes the request and sends the response back to the API.
- Delivery: The API returns the server's response to the client.
Types of API Architectures
-
REST API
- REST is "Representational State Transfer".
- A simple, flexible API architecture that uses HTTP methods (GET, POST, PUT, DELETE) for communication.
-
SOAP
- SOAP is the Simple Object Access Protocol.
- A more rigid protocol that requires XML-based messaging for communication.
What are REST APIs?
- REST stands for Representational State Transfer, and follows the constraints of REST architecture, allowing interaction with RESTful web services.
- It defines a set of functions (GET, PUT, POST, DELETE) that clients use to access server data.
- GET (retrieve a record).
- PUT (update a record).
- POST (create a record).
- 2. GET – Fetch Single User by DELETE (delete the record).
Combined REST API Example
Base URL
https://api.example.com/users
GET – Fetch All Users
GET /users
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
GET – Fetch Single User by ID
GET /users/1
{ "id": 1, "name": "Alice" }
POST – Create New User
POST /users
Content-Type: application/json
{
"name": "Charlie"
}
{ "id": 3, "name": "Charlie" }
PUT – Update Entire User Data
PUT /users/3
Content-Type: application/json
{
"name": "Charlie Brown"
}
{ "id": 3, "name": "Charlie Brown" }
PATCH – Update Partial User Data
PATCH /users/3
Content-Type: application/json
{
"name": "Charlie Updated"
}
{ "id": 3, "name": "Charlie Updated" }
DELETE – Remove User
DELETE /users/3
{ "message": "User deleted successfully" }
Final Thought on API Integration
GET
→ Read data
POST
→ Create data
PUT
/ PATCH
→ Update data
DELETE
→ Delete data