Skip to content

    Introduction

    FastAPI Course

    From the first endpoint to the API in production, with code verified by execution and a complete blog project with PostgreSQL.

    By Alexsander Valente

    Who is this material for

    FastAPI has become one of the most used ways to build APIs in Python because it combines three ideas: you describe the data with type annotations, and the framework takes from there the validation, the conversion common finished in about 0.2 seconds, because the waits overlapped. interactive documentation, without extra code. I organized these notes into five parts, from "Hello, world" to a blog API with users, JWT, and a database.

    PartFor whomWhat you will be able to do by the end
    BasicWho knows basic Python and has never made an APICreate routes, receive validated data, return well-formed responses and errors
    IntermediateWho has already created routes and needs a real APIDependencies, middleware, database with SQLAlchemy, async, authentication with JWT, upload, CORS, configuration, and tests
    AdvancedWho will deploy the APIIntegrate external APIs, paginate, cache, limit requests, and deploy
    Blog ProjectWho wants to see everything working togetherA complete API, with PostgreSQL, users, owner-based authorization, and tests
    InterviewsWho will be tested on the subjectAnswer the most common questions, with the explanation behind each answer

    If you have never programmed in Python, take the course Python Course, at least up to chapter 25. Here I assume you know what a function, a class, and a import.

    How each chapter works

    Every chapter follows the same layout: a short explanation, the code, and its result. Three rules apply to the entire course:

    • The output is real. I executed each snippet and copied what Python printed. To show the behavior of a route without needing a running server, the examples call the API with the TestClient, which makes the request in memory, exactly as Swagger or the curl would.
    • The exercises have solutions. Try first. The solution is hidden and ends with assert, so you find out for yourself if you got it right.
    • Each block indicates where it is. At the top of each snippet, the file and lines from the repository appear (for example, basico/cap08_crud_tarefas.py), and you can run it on its own.
    • I correct what has become outdated. Some popular materials on FastAPI teach practices that today are broken or insecure. When this happens, I show the problem with a real test and the current way to solve it.

    Versions that the course assumes

    The course assumes Python 3.10 or higher (the current FastAPI requires this, not the 3.8 that appears in old materials), FastAPI 0.142, Pydantic 2 and SQLAlchemy 2. For the code of the chapters:

    Terminal
    uv init minha-api
    cd minha-api
    uv add "fastapi[standard]"
    uv add --dev pytest httpx2
    

    The extra [standard] install the server (uvicorn), the command fastapi and the dependencies used in forms and upload. The httpx2 is the HTTP client that the TestClient uses in the tests. With the httpx old version, the TestClient works, but issues a deprecation warning.

    The repository

    The course code is in a repository, with an executable file per chapter and the complete blog project, organized by the same levels as the course:

    Estrutura do repositório
    fastapi-notes/
      README.md
      pyproject.toml
      docs/
        fastapi-notes.html
      basico/
      intermediario/
      avancado/
      projeto_blog/
        blog_api/
      entrevistas/
    

    Each level folder has a README.md (goal, environment, route contract) and a recording script. To get started, from the root of the repository:

    Terminal
    uv venv
    uv sync
    uv run basico/cap01_introducao.py
    

    A rule that I repeat in this course

    Everything that enters the API is unknown until validated, and everything that goes out is your decision. FastAPI does the first part for you, based on the types. The second part (what you return: no passwords, no errors disguised as success) is your responsibility, and a good part of this course is about it.