Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Andrés Cruz Yoris

Rating No ratings yet

FastAPI is a great web framework for creating web APIs with Python; It offers us multiple features with which it is possible to create modular, well-structured, scalable APIs with many options such as validations, formats, typing, among others. When you install FastAPI, two very important modules are installed: Pydantic that allows the creation of models for data validation. Starlette, which is a lightweight ASGI tooltip, used to create asynchronous (or synchronous) web services in Python. With these packages, we have the basics to create APIs, but we can easily extend a FastAPI project with other modules to provide the application with more features, such as the database, template engines, among others. FastAPI is a high-performance, easy-to-learn, start-up framework; It is ideal for creating all kinds of sites that not only consist of APIs, but we can install a template manager to return complete web pages. This book is mostly practical, we will learn the basics of FastAPI, knowing its main features based on a small application that we will extend chapter after chapter and whose content you can see below: Chapter 1: We present some essential commands to develop in FastApi , we will prepare the environment and we will give an introduction to the framework . Chapter 2: One of the main factors in FastApi is the creation of resources for the API through functions, in this section we will deal with the basics of this, introducing routing between multiple files as well as the different options for the arguments and parameters of these routes. Chapter 3: In this section, learn how to handle HTTP status codes from API methods and also handle errors/exceptions from API methods. Chapter 4: In this section we will see how to create sample data to use from the automatic documentation that FastAPI offers for each of the API methods. Chapter 5: In this chapter we will see how to implement the upload of files, knowing the different existing variants in FastAPI. Chapter 6: I

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A hands-on, chapter-by-chapter starter for building a real FastAPI application in Python, from your first "hello world" route to authentication and tests. Best for Python developers who already know the basics and want a practical, project-driven path into modern async web APIs. 【Book Arc】 - **Opening (~0%–10%)**: Sets up the environment and mental model — installing FastAPI (with Pydantic and Starlette), preparing a virtual environment, and running a first app with Uvicorn. - **Early (~10%–35%)**: Builds the core of any API: routing across multiple files, path/query/body parameters, dynamic URL parameters, and request-body validation with Pydantic models. - **Middle (~35%–55%)**: Adds production-shaped concerns — HTTP status codes and exception handling, customizing automatic docs with sample data, and file uploads (single and multiple). - **Late (~55%–85%)**: Connects the app to a relational database via SQLAlchemy/MySQL, renders HTML with the Jinja template engine, and introduces dependencies and middleware. - **Ending (~85%–100%)**: Rounds out the application with a user module (registration, login, access tokens, logout) and unit testing. 【Key Takeaways】 - **FastAPI is a thin, composable layer over Pydantic and Starlette** (Opening): installing the framework pulls in data validation and an ASGI toolkit, which together give you typed, async-capable APIs without heavy scaffolding. - **Routing is function-based and decorator-driven** (Early): each HTTP verb maps to a decorator, and routers let you split endpoints across files and mount them with prefixes for modular structure. - **Parameters live in three places — path, query, and body** (Early): choosing the right one matters, and FastAPI validates each with `Path()`, `Query()`, and `Body()` constraints, returning structured 422 errors when input is wrong. - **Pydantic models are the backbone of request validation** (Early–Middle): nested models, enums for fixed values, field validators, and list/set fields let you enforce shape and business rules declaratively. - **Status codes and exceptions are first-class** (Middle): you set response codes per route and raise handled errors, so clients get predictable, meaningful HTTP semantics. - **Automatic docs can be enriched with examples** (Middle): supplying sample payloads and value lists makes the generated OpenAPI documentation genuinely usable for testing and onboarding. - **File uploads require `python-multipart` and `UploadFile`** (Middle): single and multiple files are handled with streaming copies, with folder-existence being a common pitfall. - **Persistence, templating, auth, and tests extend the same app** (Late–Ending): SQLAlchemy/MySQL for storage, Jinja for HTML responses, dependencies and middleware for cross-cutting logic, and a user module with tokens plus unit tests to finish. 【Reading Tips】 - **Deep-read the routing and Pydantic chapters** (Early): these are the concepts everything else builds on; skim the environment setup if you already use virtualenv and Uvicorn. - **Type along with the running example**: the book extends one small task application chapter by chapter, so following the code in order pays off more than reading passively. - **Treat the docs and validation errors as a learning tool**: experiment with the `/docs` page and deliberately send bad input to see how FastAPI reports errors. - **Pause before the database and auth chapters** to make sure your models and routers are clean — those later chapters assume a well-structured project. - **Use the chapter source repositories** referenced in the excerpts to compare your code when something breaks. 【Coverage Limits】 The excerpts cover the book's map and early-to-middle chapters in detail, but later material (full database integration, Jinja usage, authentication flow, and unit testing) is only summarized; specific implementation details there are not fully represented here.
Page 11
methods and also handle errors/exceptions from API methods. Chapter 4: In this section we will see how to create sample data to use from the automatic docume...
View in text
Excerpt 2
@app.get("/") def hello_world(): return {"hello": "world"} With this, we have our first hello world example in FlastApi; but, in order to see this message on...
View in text
Excerpt 3
basically a series of classes where validations are defined and applied to the data that will be received in the user's request, therefore, the presentation...
View in text
Excerpt 4
) as buffer: shutil.copyfileobj(file.file, buffer) return { "filename" : file.filename } In Python, the with statement is used in exception handling to make...
View in text
Excerpt 5
database/pagination.py Staying like: database/pagination.py from typing import Generic, List, TypeVar from pydantic import BaseModel, conint # from pydantic....
View in text
Excerpt 6
nd you get as output: { "id": 2, "name": "Salvar al mundo", "status": "pending", "user": { "email": "admin@gmail.com", "id": 1, "name": "andres", "surname":...
View in text
Excerpt 7
<input type="hidden" name="user_id" value="1"> 93 document.querySelector('#editForm input[name="id"]').value = b.getAttribute('data-id') document.querySelect...
View in text
Excerpt 8
e user to be authenticated; for example: @app.get("/page/") def page(db: Session = Depends(get_database_session), dependencies=Depends(authenticate)): print(...
View in text
Tags
AI categories
PythonBackendWeb Technology
Publish Year: 2023
Language: English
Pages: 355
File Format: PDF
File Size: 1.5 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…