You write eleven lines of Python. You run one command. And suddenly you have a working API, input checking, clear error messages and a documentation page you never wrote.
That's FastAPI. FastAPI is a Python framework for building APIs, where your type hints do the boring work: reading the request, checking the data, and writing the docs.
But "it just works" is not a great feeling when something breaks. So let's follow one request, stop by stop, from the moment you press Enter to the moment JSON comes back.
The 11 lines
from fastapi import FastAPI from pydantic import BaseModel
app = FastAPI ( )
class Book ( BaseModel ) : title : str pages : int
@app . post ( "/books" ) def add_book ( book : Book ) : return { "saved" : book . title , "pages" : book . pages }
Enter fullscreen mode
Exit fullscreen mode
Run it:
pip install "fastapi[standard]" fastapi dev main.py
Enter fullscreen mode
Exit fullscreen mode
Now open http://127.0.0.1:8000/docs . There's a full, clickable docs page for your API.β¦