Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FastAPI: build your first app

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

FastAPI: build your first app

Build your first app using FastAPI - GDG Bari - Lightning talk nigh - 12/03/2025

Avatar for fedecarroz

fedecarroz

March 12, 2025

More Decks by fedecarroz

Other Decks in Programming

Transcript

  1. Let’s know each other • Senior Flutter Developer | Cloud

    Analyst @ Datwave • Master degree in Computer Engineering - Cybersecurity and Cloud @ PoliBa • Volunteer @ GDG Bari • F1 and MotoGP fan • Guitar player
  2. certifications 160+ projects over the last 3 years 100+ professionals

    90+ years of experience on cognitive technologies, AI and GenAI 7+ At Datwave, we leverage Google Cloud technology to deliver transformative AI, Machine Learning, and Cloud solutions. As a Premier Partner since 2016, we specialize in large-scale, data-intensive transformations that drive innovation and efficiency. empowers with Awards & Specializations Re@cK: SOC enabled by Google Tech Google Selected Partner on Conversational AI
  3. FastAPI FastAPI is a modern, high-performance web framework for building

    APIs with Python built on top of Starlette (an ASGI framework) for the web layer and Pydantic for data validation and serialization. It’s designed to help developers build fast, robust, and fully documented APIs with minimal effort and clean, type-safe code. • Asynchronous by design • Automatic data validation • Auto-generated OpenAPI and JSON Schema • High performance • Modular and extensible
  4. WSGI vs ASGI WSGI ASGI Designed for Sync apps only

    Sync + Async apps await/async support WebSockets support Background tasks Real-time communication Suitable for microservices/API Scalability under concurrency Low High
  5. Uvicorn Uvicorn is a lightweight, lightning-fast ASGI server built on

    uvloop and httptools. It is designed to run asynchronous Python web applications, especially those built with FastAPI, Starlette, Django (ASGI mode). Uvicorn acts as the bridge between the web client and your Python application, efficiently handling HTTP requests, WebSockets, and background tasks. • ASGI-compliant • Ultra-fast performance • Low resource footprint • Supports HTTP/1.1, HTTP/2, and WebSockets • Easy integration with Gunicorn
  6. Gunicorn Gunicorn (Green Unicorn) is a pre-fork WSGI HTTP server

    for UNIX-based systems, widely used to serve Python web applications in production environments. It is compatible with WSGI-based frameworks such as Django or Flask, and others, and is designed to manage multiple worker processes efficiently. Although designed for WSGI (synchronous) applications, it can also be used with ASGI apps via the UvicornWorker class, making it a popular production stack for FastAPI. • Robust and stable • Highly configurable • Multiprocess architecture for scalability • Support for multiple worker types • Advanced load balancing
  7. Pydantic Pydantic is a Python library for data validation, parsing,

    and settings management using Python type annotations. It powers the data modeling layer of FastAPI, ensuring that request and response data are correct, safe, and well-defined. It transforms complex nested structures into well-typed Python objects automatically validated and converted. • Data validation • Automatic data parsing (JSON serialization) • JSON Schema generation • Field constraints • Clear and informative error messages
  8. Swagger UI Swagger is a set of open-source tools and

    specifications for designing, documenting, testing, and consuming RESTful APIs. It is based on the OpenAPI Specification (OAS) and is the de facto standard for describing REST APIs in a machine-readable format. FastAPI auto-generates Swagger UI using OpenAPI schema from Pydantic models. • Generates interactive API documentation • Provides request/response examples • Includes built-in support for authentication mechanisms • Allows to test directly from the browser • Helps maintain consistency and versioning in APIs
  9. JWT JWT (JSON Web Token) is a compact, URL-safe standard

    used for secure authentication and authorization for data exchange between two client and server. A JWT is a stateless digitally signed token that proves the identity of a user and can carry additional claims (data). JWT structure: • Header → specifies the algorithm (e.g., HS256) and type (JWT) • Payload → contains user data (claims), e.g., sub, exp, role • Signature → ensures integrity and authenticity of the token
  10. SQLAlchemy SQLAlchemy is the most widely used Object Relational Mapper

    (ORM) in Python. It provides a full suite of tools for interacting with relational databases in an object-oriented way, allowing developers to work with Python classes instead of writing raw SQL queries. It supports both declarative ORM mapping and core SQL expression language. • SQL Expression Language support • Database agnostic (RDBMS) • Session management • Migration support via Alembic • Supports both sync and async programming
  11. The big match: FastAPI vs Django vs Flask Lightweight micro-framework

    for building APIs Full-stack framework with a dedicated library for RESTful APIs Lightweight backend micro-framework Modular approach Monolithic design Modular approach Easy to learn Steeper learing curve Easy to learn Natively asynchronous (ASGI) Mostly synchronous (WSGI → ASGI) Synchronous (WSGI) Best performance Poor performance Good performace ORM using SQLAlchemy ORM built-in ORM using SQLAlchemy DB migration using Alembic DB migration built-in DB migration using Flask-Migrate Automatic docs (OpenAPI) built-in DRF external library External library Pydantic validation and serialization DRF serializers External library Built-in OAuth2/JWT Built-in JWT (DRF) + external OAuth External library
  12. Clean architecture • core/: app config • models/: DB models

    • repositories/: DB operations • routers/: endpoints • schemas/: app data classes • services/: business logic • .env: environment variables • main.py: app entry point Repository Service Router Model Schema
  13. What’s missing? • Security • Password hashing • User authetication

    mechanism • User authorization policies • Database • Migration using Alembic • Optimization • Logging