FastAPI
FastAPI is a web framework for building HTTP-based service APIs in Python 3.8+. It uses Pydantic and type hints to validate, serialize and deserialize data. FastAPI also automatically generates OpenAPI documentation for APIs built with it. It was first released in 2018.
FastAPI is a web framework for building HTTP-based service APIs in Python 3.8+. It uses Pydantic and type hints to validate, serialize and deserialize data. FastAPI also automatically generates OpenAPI documentation for APIs built with it. It was first released in 2018. from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float is_offer: bool | None = None @app.post("/items/") def create_item(item: Item): # The 'item' object is already validated and typed return {"message": "Item received", "item_name": item.name} from fastapi import Depends, HTTPException, status from db import DbSession --- Dependency for Database Session --- def get_db(): db = DbSession() try: yield db finally: db.close() @app.post("/items/", status_code=status.HTTP_201_CREATED) def create_item(name: str, description: str, db: DbSession = Depends(get_db)): new_item = Item(name=name, description=description) db.add(new_item) db.commit() db.refresh(new_item) return {"message": "Item created successfully!", "item": new_item} @app.get("/items/{item_id}") def read_item(item_id: int, db: DbSession = Depends(get_db)): item = db.query(Item).filter(Item.id == item_id).first() if item is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found") return item You must have 'websockets' package installed from fastapi import WebSocket @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() while True: data = await websocket.receive_text() await websocket.send_text(f"Message text was: {data}") import time import shutil from fastapi import BackgroundTasks, UploadFile, File from utils import generate_thumbnail @app.post("/upload-image/") async def upload_image( image: UploadFile = File(...), background_tasks: BackgroundTasks ): file_location = f"uploaded_images/{image.filename}" # Save uploaded image with open(image_path, "wb") as f: contents = await file.read() f.write(contents) # Add thumbnail generation as a background task background_tasks.add_task(generate_thumbnail, file_location, "200x200") return {"message": f"Image '{image.filename}' uploaded. Thumbnail generation started in background."} Import FastAPI class from the fastapi package from fastapi import FastAPI Create an instance of the FastAPI app app = FastAPI() Define a GET route for the root URL ("/") @app.get("/") async def read_root() -> str: # Return a plain text response return "Hello, World!" ==External links== ==References==
References
No reference rows published for this article.
Cite this article
MLA
"FastAPI." encyclopegia.org, 2026, https://encyclopegia.org/wiki/fastapi. Accessed 24 November 2026.
Chicago
"FastAPI." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/fastapi.
APA
encyclopegia.org. (2026). FastAPI. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/fastapi
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON
Claims
| Predicate | Value | Source |
|---|---|---|
| release date | 2018 | Wikipedia |