PythonPythonAPI
Building Fast APIs with Python FastAPI
Create blazing-fast, production-ready REST APIs in Python using FastAPI with automatic OpenAPI documentation.
Mar 25, 20268 min read9,200 views1180 words
Your First FastAPI App
PY
| 1 | from fastapi import FastAPI |
| 2 | from pydantic import BaseModel |
| 3 | |
| 4 | app = FastAPI() |
| 5 | |
| 6 | class Post(BaseModel): |
| 7 | title: str |
| 8 | content: str |
| 9 | published: bool = False |
| 10 | |
| 11 | @app.get("/posts") |
| 12 | async def get_posts(): |
| 13 | return await fetch_all_posts() |
| 14 | |
| 15 | @app.post("/posts", status_code=201) |
| 16 | async def create_post(post: Post): |
| 17 | return await save_post(post) |