Skip to content

Basics

FastAPI is a modern Python web framework for building APIs quickly and efficiently. It’s built on top of Starlette (for web handling) and Pydantic (for data validation). FastAPI is known for its speed, ease of use, and automatic generation of interactive documentation using OpenAPI (Swagger).

FastAPI is a web framework designed to create RESTful APIs with minimal code and maximum performance.

  • Asynchronous support (async/await): Great for high-performance apps (like real-time APIs).
  • Data validation: Built-in using Pydantic.
  • Type hints: Used for input/output validation and automatic docs.
  • Automatic docs: Swagger UI and ReDoc generated out of the box.
  • Fast: Very close to Node.js and Go in performance benchmarks.
  • Easy to use: Minimal boilerplate code.
FeatureFastAPIDjango
API FirstDesigned for APIsWeb framework with API via Django REST
Async supportFirst-class async supportLimited (added later, not native)
PerformanceVery fast (near Node.js/Go speed)Slower due to sync model
Automatic docsYes (Swagger & ReDoc)Not out of the box
Type validationYes (via Pydantic & Python types)Manual or via Django REST serializers
BoilerplateMinimalMore boilerplate
Learning curveEasier for API-only devsSteeper for REST APIs
  • Use FastAPI if:

    • You are building a microservice or API-only backend.
    • You want modern Python type hints and async support.
    • You care about speed and automatic docs.
  • Use Django if:

    • You need an all-in-one solution with admin panel, ORM, templating.
    • You are building a full-stack site or CMS.
    • You have complex relational database needs with Django ORM.

To create a project using FastAPI for the backend and a frontend (like React, Vue, plain HTML, etc.), follow these high-level steps:

Terminal window
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate

2. Install FastAPI and a server (e.g., Uvicorn):

Section titled “2. Install FastAPI and a server (e.g., Uvicorn):”
Terminal window
pip install fastapi uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Allow requests from frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Change to your frontend URL in production
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/hello")
def hello():
return {"message": "Hello from FastAPI!"}
Terminal window
uvicorn main:app --reload

Choose a frontend stack like:

  • React (with Vite or Create React App)
  • Vue
  • Plain HTML/JS
Terminal window
npm create vite@latest my-frontend --template react
cd my-frontend
npm install
npm run dev

In your React component (e.g., App.jsx):

import { useEffect, useState } from 'react';
function App() {
const [message, setMessage] = useState("");
useEffect(() => {
fetch('http://localhost:8000/api/hello')
.then(res => res.json())
.then(data => setMessage(data.message));
}, []);
return <h1>{message}</h1>;
}
export default App;
  • Make sure the backend (FastAPI) is running on localhost:8000
  • Make sure the frontend (React) is running on localhost:5173 (or similar)
  • Backend: Deploy with Render, Railway, or a VPS (e.g., using Gunicorn + Nginx).
  • Frontend: Deploy with Vercel, Netlify, or Firebase Hosting.

FastAPI does not have its own web server built-in, but it is designed to run on ASGI servers (Asynchronous Server Gateway Interface), primarily:

ServerDescription
UvicornMost commonly used. Lightweight and fast.
HypercornAlternative ASGI server. Supports HTTP/2, QUIC.
DaphnePart of Django Channels (less common for FastAPI).

You install and run Uvicorn like this:

Terminal window
pip install uvicorn
uvicorn main:app --reload
  • main → your Python file (i.e., main.py)
  • app → the FastAPI instance (app = FastAPI())
  • --reload → auto-reload when code changes (for dev only)

FastAPI is:

  • Just a Python framework to define routes and logic.
  • ASGI-compatible (similar to how Flask is WSGI-compatible).
  • Dependent on an external ASGI server like Uvicorn to actually serve HTTP requests.

  • ❌ FastAPI doesn’t include a server.
  • ✅ You use Uvicorn (or another ASGI server) to run it.
  • ✅ This separation allows FastAPI to be asynchronous, high-performance, and flexible.

Let me know if you want help setting up Uvicorn in production (with Gunicorn or Nginx).