Topics in this subject
FastAPI 3 min read Updated 11 Aug 2026

Mental Model and ASGI

Why FastAPI? ASGI (Uvicorn) vs WSGI, and extreme speed in Python.

🧑‍🏫 Sabse pehle — simple mein samjho#

Python hamesha se AI aur Data Science ka king raha hai, par web servers banane mein wo Node.js ya Go ke mukable slow tha kyunki Python by default ek kaam khatam hone par hi dusra karta hai (Synchronous). FastAPI ne aa kar game badal diya. Isne Node.js ki tarah asynchronous I/O (async/await) ko apna liya ASGI ke zariye. Ab FastAPI Node.js aur Go jitna tez hai, aur sath hi usme Pydantic jaisi cheezein hain jo code likhte hi saare errors pakad leti hain.

What is ASGI?#

Historically, Python web frameworks like Django and Flask used WSGI (Web Server Gateway Interface). WSGI is strictly synchronous. It handles one request at a time per worker process. If a request involves a long database query, the worker is blocked.

FastAPI is built on ASGI (Asynchronous Server Gateway Interface). This allows the server to handle asynchronous code (async and await). While a database query is running in the background, the server can accept thousands of new incoming requests simultaneously (exactly like the Node.js Event Loop).

To run a FastAPI application, you don't just run the python file. You need an ASGI server, the most popular being Uvicorn.

The FastAPI Philosophy#

FastAPI is built on three core pillars:

  1. Starlette: The underlying web framework that handles the ASGI routing and asynchronous capabilities.
  2. Pydantic: The data validation library that ensures the data coming in and going out is exactly the type you expect.
  3. Type Hints: Python 3.6+ introduced type hints. FastAPI leverages these heavily. You write standard Python type hints, and FastAPI uses them to validate data and generate documentation automatically!

A Minimal FastAPI App#

# main.py
from fastapi import FastAPI

# Initialize the app
app = FastAPI()

# Define a route using a decorator
@app.get("/")
async def root():
    # FastAPI automatically converts dictionaries to JSON
    return {"message": "Hello World from FastAPI"}

Running the Server#

You don't run python main.py. Instead, you use the Uvicorn ASGI server from your terminal:

# 'main' is the filename (main.py)
# 'app' is the variable name inside the file
# '--reload' automatically restarts the server when you save code (like nodemon)
uvicorn main:app --reload

Automatic Interactive Documentation#

Because FastAPI uses Pydantic and Type Hints, it knows the exact structure of your API. It automatically generates a beautiful, interactive Swagger UI documentation page.

Once your server is running, simply go to your browser and visit: http://localhost:8000/docs

You can test all your API endpoints directly from this webpage without needing Postman!