Chapter 4: APIs (REST & GraphQL)

The Frontend (React) and the Backend (Node/Python/Database) are completely separate entities. They do not know about each other. To make them communicate, you must build a bridge. That bridge is an API (Application Programming Interface).

REST APIs: The Industry Standard

REST (Representational State Transfer) is a set of rules for building APIs. A RESTful API relies on standard HTTP Methods to perform CRUD (Create, Read, Update, Delete) operations on a database.

// The Backend returning JSON via a REST API
{
    "status": 200,
    "data": {
        "id": 5,
        "username": "msmaxpro",
        "role": "admin"
    }
}

Status Codes Matter

When a frontend client sends an API request, your backend must respond with the correct HTTP Status Code. If you send a `200 OK` code when an error occurred, the frontend code will break.

GraphQL vs REST

REST is great, but sometimes it returns too much data. If the frontend only needs a user's name, a REST API might return the name, age, email, and address. GraphQL is a modern alternative created by Facebook where the frontend can request exactly the fields it wants, and nothing else. It prevents "over-fetching".

Mini Task: Use Postman

  1. Download Postman or use the browser version.
  2. Make a GET request to a free public API (like https://pokeapi.co/api/v2/pokemon/ditto).
  3. Inspect the JSON data that comes back. This is exactly what you will be building as a backend developer!
Continue to Chapter 5: Authentication