Introduction to FastAPI

What is an API?

An API stands for Application Programming Interface. It allows different software systems to communicate with each other by transferring data in a structured way. Think of it as a bridge that lets one program access features or data from another without needing to understand its internal workings.

Use Case: Shared Backend API for Web, Android, and Separate Frontend (e.g., React):

You have an application available on Android, Web (with React as the frontend), and potentially iOS. Instead of duplicating business logic across platforms, you create a single backend that all clients communicate with through HTTP APIs.

What is FastAPI?

FastAPI is a modern, high-performance web framework for building APIs with Python, leveraging standard type hints. It delivers fast performance and automatically generates interactive API documentation. FastAPI also enables easy and reliable data validation using Pydantic.

Official Site: https://fastapi.tiangolo.com/

Python Programming Language

Folder Structure for a FastAPI Project

In a FastAPI project, the folder structure organizes different parts of the app into separate folders like models, routes, and logic. This helps keep everything clean and easy to manage. It makes the app scalable and allows developers to work on different parts without confusion.

There are two types of folder structures to organize a FastAPI project:

1. Horizontal Folder Structure

In a horizontal folder structure, the project is divided into separate folders for each feature, like models, routes, or services. Each folder is at the same level, making it easy to organize and manage different parts of the application.

Checking Python Version
app/                    # Main application directory
├── __init__.py
├── .env
├── main.py             # App entry point
│
├── db/
│   ├── __init__.py
│   └── config.py
│
├── product/
│   ├── __init__.py
│   ├── models.py       # Database models
│   ├── routes.py       # API endpoints
│   ├── schemas.py      # Pydantic schemas for validation
│   └── services.py     # Business logic or external services
│
├── user/
│   ├── __init__.py
│   ├── models.py
│   ├── routes.py
│   ├── schemas.py
│   └── services.py
│
├── tests/              # Test files
│   ├── __init__.py
│   ├── test_product.py
│   └── test_user.py
2. Vertical Folder Structure

You have an application available on Android, Web (with React as the frontend), and potentially iOS. Instead of duplicating business logic across platforms, you create a single backend that all clients communicate with through HTTP APIs.

Checking Python Version
Project/
├── app/              # Main application directory
│ ├── models/         # Database models
│ ├── routes/         # API endpoints
│ ├── utils/          # Utility functions
│ ├── services/       # Business logic or external services
│ ├── schemas/        # Pydantic schemas for validation
│ └── main.py         # App entry point
└── tests/            # Test files

Quick Installation Guide

Install Python:

To use FastAPI, you need to install Python, as it is a Python-based framework. You can download the latest version of Python from python.org and verify the installation by typing python in your terminal.

Checking Python Version
python --version

# Completely Remove Existing Python from Your System
# Step 1: Uninstall Python from Control Panel
# Step 2: Remove Python paths from Environment Variables
# Step 3: Delete leftover folders from C:\Users\< YourUser >\AppData\Local\Programs\Python
Install Virtual Environment (venv):

Use Python's built-in venv module to create a clean, isolated environment for your project, helping keep dependencies organized and free of conflicts. For a complete guide, visit the Virtual Environment Full Guide page.

FastAPI Setup Commands
# Create virtual environment
python -m venv my_project_env

# Activate (use the appropriate command based on your OS)
my_project_env\Scripts\activate   # Windows

# source my_project_env/bin/activate  # macOS/Linux

# Install dependencies
pip install fastapi

# Deactivate when done
deactivate
Install FastAPI:

You can install FastAPI in two common ways, depending on your project needs: install fastapi and uvicorn separately for a minimal setup, or use the standard installation with fastapi[standard] to include additional required tools.

FastAPI Setup Commands
# Install FastAPI and Uvicorn
pip install fastapi uvicorn

# Install FastAPI with extra dependencies
pip install "fastapi[standard]"

# Check if FastAPI is installed
python -m fastapi --version

Exercise

  1. Fill in the Blanks:
    • - Django is a __________ written in Python that simplifies web development.
    • - Django follows the __________ architecture, which stands for __________.
    • - To install Django, the package manager used is __________.
    • - You can check the installed Django version using the command __________.
    • - MVC stands for __________.