FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints. It's designed for rapid development and boasts exceptional performance, comparable to NodeJS and Go, thanks to its foundation on Starlette for the web parts and Pydantic for data validation and serialization. The term 'Hızlı API Sunucusu' directly translates to 'Fast API Server', which perfectly encapsulates FastAPI's core promise.
Key Features and Why it's 'Fast':
1. High Performance: FastAPI leverages ASGI (Asynchronous Server Gateway Interface) and asynchronous programming (`async`/`await`), allowing it to handle a large number of concurrent requests efficiently. When run with an ASGI server like Uvicorn, it achieves very high throughput.
2. Developer Productivity: It's designed to minimize development time. By using Python type hints, FastAPI automatically handles request body validation, serialization, and deserialization using Pydantic models. This reduces boilerplate code and common errors.
3. Automatic API Documentation: One of its standout features is the automatic generation of interactive API documentation. Based on the OpenAPI standard, it provides Swagger UI and ReDoc interfaces directly from your code, making API testing and collaboration incredibly efficient.
4. Type Hinting: FastAPI extensively uses standard Python type hints. This enables excellent editor support (autocompletion, type checking) and helps catch errors during development, leading to more robust code.
5. Data Validation and Serialization: Pydantic models are at the heart of FastAPI's data handling. They provide robust data validation, automatic serialization to JSON, and clear error messages out of the box.
6. Dependency Injection System: It includes a powerful and easy-to-use dependency injection system, simplifying the management of common resources like database connections, authentication, and external services.
7. Security: FastAPI provides tools for integrating various security schemes, including OAuth2, bearer tokens, and more.
Underlying Technologies:
- Starlette: Provides the core ASGI web framework capabilities, including routing, middleware, and request/response handling.
- Pydantic: Handles data validation, settings management, and serialization using Python type hints.
- Uvicorn: A lightning-fast ASGI server that runs the FastAPI application.
Benefits:
- Reduced Development Time: Type hints, automatic validation, and documentation significantly speed up the development cycle.
- Fewer Bugs: Strong type checking and data validation reduce common API-related errors.
- Easy Maintenance: Clean, type-hinted code is easier to read, understand, and maintain.
- Excellent Performance: Suitable for high-load applications requiring fast response times.
In essence, FastAPI allows developers to build high-performance, production-ready APIs with Python in a highly efficient and enjoyable manner.
Example Code
First, install necessary libraries:
pip install fastapi "uvicorn[standard]" pydantic
main.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional, Dict
1. Initialize FastAPI app
app = FastAPI(
title="My Fast API Example",
description="A simple API demonstrating FastAPI features.",
version="1.0.0",
)
2. Define a Pydantic model for request/response body validation
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
In-memory database for demonstration
fake_db: Dict[int, Item] = {}
item_id_counter = 0
3. Define a GET endpoint
@app.get("/", tags=["Root"])
async def read_root():
"""
Returns a simple welcome message.
"""
return {"message": "Welcome to the FastAPI example!"}
4. Define a GET endpoint with a path parameter
@app.get("/items/{item_id}", response_model=Item, tags=["Items"])
async def read_item(item_id: int):
"""
Retrieves a single item by its ID.
"""
if item_id not in fake_db:
raise HTTPException(status_code=404, detail="Item not found")
return fake_db[item_id]
5. Define a POST endpoint with a request body using Pydantic model
@app.post("/items/", response_model=Item, status_code=201, tags=["Items"])
async def create_item(item: Item):
"""
Creates a new item.
"""
global item_id_counter
item_id_counter += 1
fake_db[item_id_counter] = item
In a real application, you might return the item with its generated ID from the DB
return item
6. Define a PUT endpoint to update an item
@app.put("/items/{item_id}", response_model=Item, tags=["Items"])
async def update_item(item_id: int, item: Item):
"""
Updates an existing item by its ID.
"""
if item_id not in fake_db:
raise HTTPException(status_code=404, detail="Item not found")
fake_db[item_id] = item
return item
To run this application:
1. Save the code as 'main.py' in a directory.
2. Open your terminal/command prompt in that directory.
3. Run the following command:
uvicorn main:app --reload
Once the server is running, open your web browser and go to:
- http://127.0.0.1:8000/ (For the welcome message)
- http://127.0.0.1:8000/docs (For interactive API documentation - Swagger UI)
- http://127.0.0.1:8000/redoc (For alternative API documentation)








Fast API Server with FastAPI