Building a REST API with FastAPI

Learn how to develop and serve a REST service using the popular FastAPI library.

FastAPI is the most popular Python framework for building fast, maintainable REST APIs.
2 days
ansible python
ansible fastapi
Python developers with at least 1 year of experience
Learn how to develop and serve a REST service using the popular FastAPI library.
During the course, participants work on a REST API project of their own choosing. At regular intervals new theory will be introduced, which they will put into practice in the project.
Need this for your dev team?

Outline

Introduction

FastAPI is a relatively new framework for building REST services, which has taken the Python world by storm, and by now it is the most popular choice for implementing an API.

This course covers all main features of FastAPI and it touches on all stages of development, including testing and deployment.

Of course everything about this course can be customized to your needs.

Prerequisites

  • Basic Python knowledge (functions, standard datatypes, type hints, etc.)
  • Basic understanding of HTTP and REST
  • Basic understanding of relational databases

Day 1, Morning

  • Recap of basic Python concepts (if necessary)
  • Recap of type hints, data classes, Pydantic data classes
  • Introduction to SQLAlchemy or SQLModel (if necessary)
  • Introduction to FastAPI
  • Comparison of FastAPI with other similar libraries
  • Installation of FastAPI and Uvicorn
  • Configuration of the development environment
  • Starting and debugging a FastAPI application
  • Implementing a simple GET operation

Day 1, Afternoon

  • Consuming a GET operation using Postman
  • Understanding main components such as models, routers, and dependencies
  • Implementing POST, PUT, PATCH, DELETE operations
  • Operations that receive information via request body and update the database accordingly
  • Difference between Path Parameters and Query Parameters and when to use them
  • Implementing operations with Path/Query parameters
  • Returning the correct data based on Path/Query parameters
  • Type declarations, conversions, and validation for Path and Query parameters
  • Required and optional parameters

Day 2, Morning

  • Using Pydantic models for the structure of the response body
  • Using Pydantic for validating the request body
  • Using separate schemas for input and output and why this is necessary
  • Best practices for input and output models
  • Complex and nested data structures with Pydantic and how to serve them with FastAPI
  • Auto-generated API schemas and how clients can use them
  • Automatic documentation using Swagger UI
  • The importance of documented APIs for developers
  • Use of doc tags for better organization of documentation
  • Cookies and headers
  • Returning correct HTTP status codes
  • Debugging and Exception Handling
  • HTML form input and file uploads
  • Understanding the role of middleware
  • Using CORS middleware
  • Serving static files

Day 2, Afternoon

  • Dependency Injection in FastAPI
  • Establishing and injecting a database connection
  • Implementing database models
  • Integrating GET/POST/PUT/DELETE with the database
  • Distinction between Database model vs Pydantic schema
  • Reorganizing code using APIRouter
  • Using the pytest unittest library in combination with FastAPI
  • Unit testing of operations
  • Integration testing with the built-in test client
  • Implementing a User Model
  • Storing passwords (hashing)
  • Authentication: Basic Auth, JWT Access Tokens, OAuth2
  • Adding Authentication to operations
  • Deploying a FastAPI app
  • Hosting options (cloud based, self hosted, etc)
  • Deployment best practices (i.e. dotenv)
  • Importance of HTTPS and how to implement
  • Requirements management
Adapt this course to fit your needs