FastAPI is one of the fastest python frameworks. If you are new to FastAPI, here you will learn more about FastAPI and you will write your first FastAPI route in less than 10 minutes.
Table of Contents
What is FastAPI?
FastAPI is a modern python web framework that is used to build high-performance APIs (Application Programming Interface).
Basic Building blocks of FastAPI.
There are two giants that FastAPI succeeded to combine that put it among the top-rated and fastest python framework for API development. The two giants are Starlette and Pydantic.
- Starlette is a lightweight ASGI framework that is ideal for running async web services in Python. Documentation
- Pydantic on the other side, is a library used for automatic data validation and serialization. In python, it is used to validate and serialize data with respect to python data types and objects respectively. Documentation
Main Features of FastAPI
-
Fast
FastAPI is very fast due to its various advantages such as
- Automatic data validation
- Automatic serialization, ie. Converts python objects to JSON format and vice versa
- Native async operations support (Non-blocking Operations)
- Automatic Documentation
- Fewer Bugs Reduce about 40% of human developer errors.
- Intuitive Great Editor support, auto-completion and less time debugging.
- Robust Get Production ready code with an automatic documentation.
- Standard-based FastAPI follows open standards for APIs such as: OpenAPI and JSON schema.
Your First FastAPI Route.
Setting Up environment on Linux.
- Open your terminal and run the following commands
# Put your system up to date
sudo apt update && sudo apt upgrade
# Install the packages globally
sudo apt install python3 python3-pip python3-venv
# Create a dedicated folder
mkdir intro_fastapi
# Navigate to the folder
cd intro_fastapi
# Create a virtual environment
python3 -m venv .venv
# Activate the virtual environment
source .venv/bin/activate
# Install the required packages to get starter
pip install "fastapi[standard]" "uvicorn[standard]"
Setting Up environment on Windows.
- Open your powershell and run the following commands
# Check Python installation
python --version
# If Python is not installed, download it from:
# https://www.python.org/downloads/windows/
# (Make sure to check "Add Python to PATH" during installation)
# Create a dedicated folder
mkdir intro_fastapi
# Navigate to the folder
cd intro_fastapi
# Create a virtual environment
python -m venv .venv
# Activate the virtual environment
.venv\Scripts\activate
# Upgrade pip (recommended)
python -m pip install --upgrade pip
# Install the required packages to get started
pip install "fastapi[standard]" "uvicorn[standard]"
Writing Your First API route
- Make sure you are at the project folder root.
- Open your folder with your favourite editor
- File Structuring
Create the following file structure
intro_fastapi/
└── app/
└── main.py
- Open main.py and write the following code
from fastapi import FastAPI
# Create an instance of the FastAPI Class
app = FastAPI()
# register your app's first route using the app decorator
@app.get("/")
def welcome():
return {"message": "My First API route"}
- Run the app (In terminal)
uvicorn app.main:app --reload
- Note: Always make sure your virtual environment is activated.
- visit the url http://127.0.0.1:8000
Automatic Documentation
FastAPI provides automatic documentation. There are of two types:
Swagger (for testing APIs): http://127.0.0.1:8000/docs

Redoc: http://127.0.0.1:8000/redoc


Top comments (0)