FastAPI Overview
================
1. What is FastAPI?
-------------------
- FastAPI is a modern, fast (high-performance) web framework for building APIs with
Python 3.7+.
- It is based on standard Python type hints, which enables automatic validation,
serialization, and interactive API documentation.
- Designed for speed, ease of use, and automatic generation of OpenAPI (Swagger)
documentation.
2. Key Features
---------------
- Fast: One of the fastest Python frameworks due to Starlette and Pydantic.
- Type hints & validation: Automatic request validation based on Python types.
- Interactive API docs: Automatically generated via Swagger UI and ReDoc.
- Dependency injection: Supports clean, modular code.
- Asynchronous support: Fully supports async/await for concurrency.
- Extensible: Easily integrates with databases, OAuth2, background tasks, etc.
3. Typical Use Cases
-------------------
- Building RESTful APIs.
- Microservices.
- Backend for web and mobile applications.
- Real-time data streaming APIs.
4. Server Used
--------------
- FastAPI runs on ASGI servers, the most common being **Uvicorn**.
- Uvicorn is a lightning-fast ASGI server that supports asynchronous requests.
- Installation:
pip install fastapi uvicorn
- Basic command to run a FastAPI app:
uvicorn main:app --reload
where:
- main → Python file (main.py)
- app → FastAPI app instance
- --reload → auto-reloads on code changes (for development)
5. How to Start Running a FastAPI App
-------------------------------------
1. Create a Python file (e.g., main.py) and define your FastAPI app:
```python
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Hello, FastAPI!"}
```
2. Open terminal/PowerShell and navigate to the project folder.
3. Run the app using Uvicorn:
uvicorn main:app --reload
4. Open a browser and visit:
http://127.0.0.1:8000
5. For interactive API docs, visit:
http://127.0.0.1:8000/docs (Swagger UI)
http://127.0.0.1:8000/redoc (ReDoc)