Building Python Web APIs with FastAPI

Book description

Discover FastAPI features and best practices for building and deploying high-quality web APIs from scratch

Key Features

  • A practical guide to developing production-ready web APIs rapidly in Python
  • Learn how to put FastAPI into practice by implementing it in real-world scenarios
  • Explore FastAPI, its syntax, and configurations for deploying applications

Book Description

RESTful web services are commonly used to create APIs for web-based applications owing to their light weight and high scalability. This book will show you how FastAPI, a high-performance web framework for building RESTful APIs in Python, allows you to build robust web APIs that are simple and intuitive and makes it easy to build quickly with very little boilerplate code.

This book will help you set up a FastAPI application in no time and show you how to use FastAPI to build a REST API that receives and responds to user requests. You'll go on to learn how to handle routing and authentication while working with databases in a FastAPI application. The book walks you through the four key areas: building and using routes for create, read, update, and delete (CRUD) operations; connecting the application to SQL and NoSQL databases; securing the application built; and deploying your application locally or to a cloud environment.

By the end of this book, you'll have developed a solid understanding of the FastAPI framework and be able to build and deploy robust REST APIs.

What you will learn

  • Set up a FastAPI application that is fully functional and secure
  • Understand how to handle errors from requests and send proper responses in FastAPI
  • Integrate and connect your application to a SQL and NoSQL (MongoDB) database
  • Perform CRUD operations using SQL and FastAPI
  • Manage concurrency in FastAPI applications
  • Implement authentication in a FastAPI application
  • Deploy a FastAPI application to any platform

Who this book is for

This book is for Python developers who want to learn FastAPI in a pragmatic way to create robust web APIs with ease. If you are a Django or Flask developer looking to try something new that's faster, more efficient, and produces fewer bugs, this FastAPI Python book is for you. The book assumes intermediate-level knowledge of Python programming.

Table of contents

  1. Building Python Web APIs with FastAPI
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Part 1: An Introduction to FastAPI
  7. Chapter 1: Getting Started with FastAPI
    1. Technical Requirement
    2. Git basics
      1. Installing Git
      2. Git operations
      3. Git branches
    3. Creating isolated development environments with Virtualenv
      1. Creating a virtual environment
      2. Activating and deactivating the virtual environment
    4. Package management with pip
      1. Installing pip
    5. Setting up Docker
      1. Dockerfile
    6. Building a simple FastAPI application
    7. Summary
  8. Chapter 2: Routing in FastAPI
    1. Technical requirements
    2. Understanding routing in FastAPI
      1. Routing example
    3. Routing with the APIRouter class
    4. Validating request bodies using Pydantic models
      1. Nested models
    5. Path and query parameters
      1. Path parameters
      2. Query parameters
    6. Request body
      1. FastAPI Automatic Docs
    7. Building a simple CRUD app
    8. Summary
  9. Chapter 3: Response Models and Error Handling
    1. Technical requirements
    2. Understanding responses in FastAPI
      1. What is a response header?
      2. What is a response body?
      3. Status codes
    3. Building response models
    4. Error handling
    5. Summary
  10. Chapter 4: Templating in FastAPI
    1. Technical requirements
    2. Understanding Jinja
      1. Filters
      2. Using if statements
      3. Loops
      4. Macros
      5. Template inheritance
    3. Using Jinja templates in FastAPI
    4. Summary
  11. Part 2: Building and Securing FastAPI Applications
  12. Chapter 5: Structuring FastAPI Applications
    1. Technical requirements
    2. Structuring in FastAPI applications
      1. Building an event planner application
      2. Implementing the models
      3. Implementing routes
    3. Summary
  13. Chapter 6: Connecting to a Database
    1. Technical requirements
    2. Setting up SQLModel
      1. Tables
      2. Rows
      3. Sessions
    3. Creating a database
      1. Creating events
      2. Read events
      3. Update events
      4. Delete event
    4. Setting up MongoDB
      1. Document
      2. Initializing the database
    5. CRUD operations
      1. Create
      2. Read
      3. Update
      4. Delete
      5. routes/events.py
      6. routes/users.py
    6. Summary
  14. Chapter 7: Securing FastAPI Applications
    1. Technical requirements
    2. Authentication methods in FastAPI
      1. Dependency injection
      2. Creating and using a dependency
    3. Securing the application with OAuth2 and JWT
      1. Hashing passwords
      2. Creating and verifying access tokens
      3. Handling user authentication
    4. Updating the application
      1. Updating the user sign-in route
      2. Updating event routes
      3. Updating the event document class and routes
    5. Configuring CORS
    6. Summary
  15. Part 3: Testing And Deploying FastAPI Applications
  16. Chapter 8: Testing FastAPI Applications
    1. Technical requirements
    2. Unit testing with pytest
      1. Eliminating repetition with pytest fixtures
    3. Setting up our test environment
    4. Writing tests for REST API endpoints
      1. Testing the sign-up route
      2. Testing the sign-in route
      3. Testing CRUD endpoints
      4. Testing READ endpoints
      5. Testing the CREATE endpoint
      6. Testing the UPDATE endpoint
      7. Testing the DELETE endpoint
    5. Test coverage
    6. Summary
  17. Chapter 9: Deploying FastAPI Applications
    1. Technical requirements
    2. Preparing for deployment
      1. Managing dependencies
      2. Configuring environment variables
    3. Deploying with Docker
      1. Writing the Dockerfile
      2. Building the Docker image
      3. Deploying our application locally
      4. Running our application
    4. Deploying Docker images
      1. Deploying databases
    5. Summary
    6. Why subscribe?
  18. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Building Python Web APIs with FastAPI
  • Author(s): Abdulazeez Adeshina
  • Release date: July 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801076630