← Back to FastAPI map
FastAPI · Foundations

Setup

Install FastAPI, start it on your machine, and lay out the files.

start here

Overview

FastAPI is a Python framework for building APIs. It sits on Starlette, which handles requests, routing and WebSockets, and Pydantic, which validates data. You write ordinary functions with type hints; FastAPI uses those hints to parse and validate input, serialize output, and generate an OpenAPI schema that powers the interactive docs at /docs. The app runs on an ASGI server such as Uvicorn.

Key concepts

ASGI
The async successor to WSGI - the interface between the server and your app. It lets one process serve many concurrent connections, including WebSockets.
Uvicorn
The ASGI server that listens on a port and hands each request to your app. The fastapi dev and fastapi run commands start it for you.
"fastapi[standard]"
Installs FastAPI plus the usual extras: Uvicorn, the fastapi CLI, python-multipart for forms and uploads, email-validator, httpx and Jinja2.
OpenAPI docs
Generated from your code: Swagger UI at /docs, ReDoc at /redoc, and the machine-readable schema at /openapi.json, which clients can generate SDKs from.

Best practices

  • Use a virtual environment per project and pin dependency versions.
  • Use fastapi dev (auto-reload) while developing; in production run without --reload.
  • Start with a single main.py and split into routers once it covers more than one resource.

Installation

Install options

Start here
# Recommended for beginners (includes uvicorn, multipart, email-validator) pip install "fastapi[standard]" # Minimal pip install fastapi uvicorn # Full dev stack pip install fastapi "uvicorn[standard]" sqlalchemy asyncpg \ pydantic-settings pyjwt "pwdlib[argon2]" \ python-multipart httpx pytest pytest-asyncio

Tip: Quote anything with square brackets. In zsh (the default macOS shell) an unquoted uvicorn[standard] is treated as a file pattern and the install fails.

Run the server

Dev
# Development - auto-reload on save uvicorn main:app --reload # Custom host + port uvicorn main:app --reload --host 0.0.0.0 --port 8080 # New FastAPI CLI (v0.111+) fastapi dev main.py # dev mode fastapi run main.py # production mode # Production with Gunicorn + multiple workers gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker # Auto docs - open in browser # http://127.0.0.1:8000/docs <- Swagger UI # http://127.0.0.1:8000/redoc <- ReDoc # http://127.0.0.1:8000/openapi.json

Project structure

Layout
# Small app myapi/ ├── main.py # everything here └── requirements.txt # Medium app myapi/ ├── main.py # app factory + router includes ├── routers/ │ ├── users.py │ └── items.py ├── schemas.py # Pydantic models ├── models.py # SQLAlchemy models ├── database.py # DB session └── .env # Large app (domain-driven) myapi/ ├── main.py ├── core/ │ ├── config.py # pydantic-settings │ └── security.py # JWT, hashing ├── api/v1/ │ ├── router.py │ └── endpoints/ ├── models/ # ORM ├── schemas/ # Pydantic ├── services/ # business logic ├── repositories/ # data access └── tests/

Comments

Sign in to leave a comment. Your name and photo come from Google; nothing else is shared.

Loading comments...