REST API Explained with Examples


A Beginner's Guide to Understanding Web APIs in Simple Words

If you’ve been learning web development, you’ve probably heard of REST APIs. But what exactly are they, and how do they work?

In this blog, we’ll break down what a REST API is, how it functions, and give you real examples so you can understand how websites, apps, and servers communicate with each other.


📘 What is a REST API?

REST stands for Representational State Transfer.
An API is an Application Programming Interface — a way for two systems (like a frontend and backend) to talk to each other.

So a REST API is a set of rules that lets your application talk to a server over HTTP (just like your browser talks to websites).


🧠 Key Concepts of REST API:

  1. HTTP Methods:

    • GET: Retrieve data (e.g., get all users)

    • POST: Send new data (e.g., create a user)

    • PUT: Update existing data

    • DELETE: Remove data

  2. Endpoints:
    A URL path that represents a resource.
    Example:
    https://api.example.com/users → users endpoint

  3. JSON Format:
    REST APIs typically send and receive data in JSON format.


💡 Example 1: Get All Users (GET Request)

http
GET https://api.example.com/users

Response:

json
[
{ "id": 1, "name": "Alice" }, { "id": 2, "name": "Bob" } ]

This returns a list of all users from the server.


📝 Example 2: Add a New User (POST Request)

http
POST https://api.example.com/users
Content-Type: application/json { "name": "Charlie" }

Response:

json
{ "id": 3, "name": "Charlie", "status": "created" }

The server creates a new user and returns confirmation.


🧱 How You Use REST APIs in JavaScript

javascript
// Fetching data from an API
fetch("https://api.example.com/users") .then(response => response.json()) .then(data => console.log(data)) .catch(err => console.error("Error:", err));

🧪 Real-Life REST API Examples

ServiceAPI ExamplePurpose
OpenWeatherapi.openweathermap.org/data/2.5/weatherGet live weather data
GitHubapi.github.com/users/{username}Fetch GitHub profiles
JSONPlaceholderjsonplaceholder.typicode.com/postsFake online REST API for testing

🔐 Authentication in REST APIs

Some APIs are public. Others need a token or API key to use them:

http
GET https://api.example.com/data
Headers: Authorization: Bearer YOUR_API_KEY

🔄 Request & Response Cycle

  1. Client (you) sends a request to an endpoint

  2. Server processes it and returns a response

  3. Response is usually in JSON and includes status codes like:

    • 200 OK – Success

    • 404 Not Found – Resource doesn’t exist

    • 500 Internal Server Error – Something went wrong


Why REST APIs Are Important

  • 🌐 Power most websites and apps today

  • 🧩 Used to connect frontend with backend

  • 🔌 Allow developers to reuse and integrate features

  • 🧠 Great to learn before building full-stack apps


📦 Conclusion

Understanding REST APIs is essential for any modern developer. Whether you’re building a frontend website or a mobile app, REST APIs help you connect to the outside world and create dynamic, data-driven experiences.


💬 Want a tutorial on building your own REST API with Python or Node.js? Drop a comment and I’ll write one for you!

Post a Comment

Post a Comment (0)

Previous Post Next Post