What is an API?
- API = Application Programming Interface.
- It’s like a waiter in a restaurant: you (the client) place an order, the waiter (API) takes it to the kitchen (server), and brings the food (response) back to you.
2. What is REST?
- REST (Representational State Transfer) is a set of rules for building web APIs.
- REST APIs use HTTP methods (like GET, POST, PUT, DELETE) to interact with resources.
- Resources are usually represented as JSON objects.
3. Common HTTP Methods in REST APIs
- GET → Retrieve data (e.g., fetch a list of users).
- POST → Send new data (e.g., create a new account).
- PUT/PATCH → Update existing data (e.g., edit profile details).
- DELETE → Remove data (e.g., delete a comment).
4. Example of a REST API Request
GET https://api.example.com/users/1
Response:
{
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}
5. How to Use REST APIs
- Find the API documentation (e.g., Twitter API, GitHub API).
- Get an API key (if authentication is required).
- Send requests using tools like:
- Browser (for simple GET requests).
- Postman (testing APIs).
- Fetch in JavaScript:
fetch("https://api.example.com/users") .then(response => response.json()) .then(data => console.log(data));
- Handle responses → usually in JSON format.
6. Real-Life Examples of REST APIs
- Weather App → Fetch current weather data from an API.
- Social Media → Posting a tweet or fetching Instagram posts.
- E-commerce → Add to cart, checkout, get product details.
