How do you create a basic FastAPI application?
Answer
Create a FastAPI app: from fastapi import FastAPI; app = FastAPI(); @app.get('/') async def root(): return {'message': 'Hello World'}. Run with Uvicorn: uvicorn main:app --reload. FastAPI automatically generates Swagger UI at /docs and ReDoc at /redoc. Use type hints for automatic validation: @app.get('/items/{item_id}') async def get_item(item_id: int, q: str = None): return {'id': item_id, 'q': q} — FastAPI validates that item_id is an integer, returns a 422 error if it isn't. Query parameters are declared as function arguments with defaults. Response models are Pydantic classes. The combination of type hints + Pydantic + automatic documentation makes FastAPI one of the most productive Python frameworks for API development.
Previous
How do you create a basic Flask application?
Next
What is Pydantic and how does it work in FastAPI?