Mastering REST APIs with FastAPI

Video description

In this course, you will master FastAPI, one of the most efficient frameworks for building APIs. Starting with an introduction to API fundamentals and the REST architecture, you'll quickly move to creating your first FastAPI application. The course then delves into organizing and structuring your project, ensuring your API is scalable and maintainable. You will learn to implement social media features, handle posts and comments, and manage various aspects of API routing.

As you progress, you'll learn pytest, a robust testing framework. You'll write tests for your FastAPI applications, ensuring your code is reliable and bug-free. The following sections focus on integrating async databases, configuring environments, and leveraging Pydantic for configuration management. You will also explore logging, user authentication with JWTs, managing many-to-many relationships, and confirming user emails through various tools and services.

The course concludes with advanced topics such as file uploads, background tasks for image generation, and deploying FastAPI applications. You'll learn to update your projects to the latest versions of Pydantic, deploy your app to platforms like Render, and manage errors with Sentry. The course also includes continuous integration with GitHub Actions, streamlining and automating your development process.

What you will learn

  • Build and deploy scalable FastAPI applications
  • Implement robust testing strategies with pytest
  • Integrate async databases and manage configurations
  • Enhance applications with advanced logging and authentication
  • Execute background tasks and manage file uploads
  • Deploy applications to cloud platforms and implement CI/CD

Audience

This course is designed for backend developers, software engineers, and technical professionals with a basic understanding of Python and web development. Familiarity with REST APIs and fundamental database concepts is recommended.

About the Author

Jose Salvatierra Fuentes: Jose Salvatierra is the founder of Teclado and a seasoned software engineer with over seven years of experience in online teaching. Passionate about helping students learn to code, Jose aims to demystify software development and make it accessible to everyone. His teaching philosophy focuses on ensuring students truly understand the intricacies of coding, making the learning process intuitive and rewarding. Specializing in Python and JavaScript, Jose is adept in web and backend development, utilizing frameworks such as Flask, React, React Native, and AngularJS. He also has extensive experience with UNIX systems, MongoDB, PostgreSQL, and advanced system architecture design.

Table of contents

  1. Chapter 1 : Course Introduction
    1. Community
    2. Welcome to this course!
    3. What is an API?
    4. What is REST?
  2. Chapter 2 : Working with FastAPI
    1. Introduction to this section
    2. Your First FastAPI App
    3. Initial App Setup
    4. Linting, formatting and sorting imports
    5. Our social media API: adding posts
    6. Splitting our API into files with APIRouter
    7. Adding comments to the social media API
  3. Chapter 3 : Introduction to pytest
    1. Introduction to the pytest section
    2. The basics of pytest
    3. Getting started with FastAPI tests
    4. Creating posts in our tests
    5. Adding tests for posts
    6. Adding comments tests
  4. Chapter 4 : Working with async databases
    1. Introduction to working with databases
    2. Installing requirements for async databases in FastAPI
    3. Creating a config file using Pydantic
    4. Different configurations per environment
    5. Config caching and how to get the config object
    6. Async database setup with FastAPI
    7. Database connection with lifespan events in FastAPI
    8. Run your FastAPI test in 'test' mode
    9. Using a database in our FastAPI router
  5. Chapter 5 : Logging in FastAPI applications
    1. Introduction to logging
    2. Python logging: loggers, handlers, and formatters
    3. Logger hierarchies and name
    4. Adding Logging Configuration for FastAPI Applications
    5. How to configure multiple loggers in the logging module
    6. Adding File Handlers for Saving Logs
    7. Add logging to your FastAPI endpoints
    8. Python logging: filters and custom filters
    9. Logging HTTPExceptions with an Exception Handler
    10. Identifying logs from the same request: Correlation ID
    11. Adding JSON-formatted log files
    12. Obfuscating email addresses in logs using a custom filter
    13. Adding Logtail for Cloud Logging in FastAPI
    14. Enabling Logtail only in production
  6. Chapter 6 : User authentication with FastAPI
    1. Introduction to user authentication
    2. Installing requirements and, what are JWTs?
    3. Adding a users table and retrieving users by email
    4. Adding user registration and tests
    5. Adding tests for the user registration endpoint
    6. How to hash passwords with passlib
    7. Generate the access token
    8. Retrieve the current user with their token
    9. Use the current user in the API router
    10. Dependency injection: getting the user
    11. Adding user relationships to other tables
    12. (Optional) OAuth Password Bearer and Swagger Auth
  7. Chapter 7 : Many-to-many relationships
    1. Adding a table for post likes
    2. Adding an API route to like posts
    3. Extracting reusable queries with SQLAlchemy
    4. Query string arguments and data sorting with Enum
  8. Chapter 8 : User email confirmation
    1. Creating the confirmation token
    2. Decoding the confirmation token
    3. Adding a user confirmation endpoint
    4. Requiring user confirmation for authenticated requests
    5. Mailgun set-up and configuration
    6. Sending emails and testing with Python
    7. Sending a confirmation email on registration
    8. Sending emails with background tasks
  9. Chapter 9 : File Uploads with FastAPI
    1. Our configuration for Backblaze B2
    2. Internal library for Backblaze B2
    3. Writing our file upload endpoint
    4. Writing tests for file upload
  10. Chapter 10 : Background Tasks for Image Generation
    1. Model and database changes for image generation
    2. Configuration for DeepAI, our third-party service
    3. Generating images using background tasks
    4. Executing image generation in our FastAPI endpoint
  11. Chapter 11 : FastAPI Deployments and Application Management
    1. Updating our project to Pydantic v2
    2. Deploy a FastAPI app to Render
    3. Add a free PostgreSQL database to your FastAPI app
    4. Error management with Sentry for FastAPI apps
    5. Continuous Integration with GitHub Actions for Python apps

Product information

  • Title: Mastering REST APIs with FastAPI
  • Author(s): Jose Salvatierra Fuentes
  • Release date: June 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781835464694