All articles
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
1from fastapi import FastAPI
2from pydantic import BaseModel
3 
4app = FastAPI()
5 
6class Post(BaseModel):
7 title: str
8 content: str
9 published: bool = False
10 
11@app.get("/posts")
12async def get_posts():
13 return await fetch_all_posts()
14 
15@app.post("/posts", status_code=201)
16async def create_post(post: Post):
17 return await save_post(post)