Why FastAPI?

Introduzione a FastAPI

Matt Eckerle

Software and Data Engineering Leader

What is FastAPI?

Let's start with some terminology

  1. API: Application Programming Interface - refers to web applications using the HTTP protocol to transmit structured data
  2. Web Application: application that serves traffic over the web
  3. Web Framework: software framework that helps build web applications

FastAPI is a fast way to build high-performance APIs using Python

Introduzione a FastAPI

FastAPI key features

  • Fast: Very high performance
  • "Low code" and easy to learn: Python annotations and type hints
  • Robust: Production-ready code with autodoc
  • Standards-based: Based on OpenAPI and JSON Schema

FastAPI logo

OpenAPI logo

JSON logo

1 https://fastapi.tiangolo.com/
Introduzione a FastAPI

FastAPI vs. other Python web frameworks

Flask

  • Build web-based (GUI) apps
  • ORM optional

FastAPI

  • Build APIs
  • ORM optional

Django

  • Build web-based (GUI) apps
  • ORM built in

Key differences

  • For APIs without database operations
  • Data and machine learning transactions
Introduzione a FastAPI

Building our first web application with FastAPI

1. Install FastAPI

pip install fastapi

2. Create your app in main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello World"}

3. Run the server

fastapi dev main.py

REPL showing FastAPI running

Introduzione a FastAPI

Before we practice with FastAPI

Some notes

  1. Can't run the FastAPI server with the "Run this code" button
  2. Define server code in the Python editor as main.py instead

  3. Run it from the terminal using the command fastapi dev main.py

  4. Verify that the logs in the terminal show Application startup complete.
  5. Stop the live server by pressing Control + C in the same terminal
  6. You should install FastAPI in your own Python environment to get used to practicing there as well
Introduzione a FastAPI

Let's practice!

Introduzione a FastAPI

Preparing Video For Download...