Sufast/Documentation
Routing
Advanced Routing
Master Sufast's powerful routing system with path parameters, query strings, and route organization.
HTTP Method Decorators
All supported HTTP methods with examples
| Method | Decorator | Use Case |
|---|---|---|
| GET | @app.get() | Retrieve data |
| POST | @app.post() | Create new resources |
| PUT | @app.put() | Update entire resources |
| PATCH | @app.patch() | Partial updates |
| DELETE | @app.delete() | Remove resources |
Path Parameters
Extract values from URL paths with automatic type conversion
Basic Path Parameters
Path Parameterspython
@app.get("/users/{user_id}")
def get_user(user_id: str):
return {"user_id": user_id}
@app.get("/posts/{post_id}")
def get_post(post_id: int): # Automatic int conversion
return {"post_id": post_id, "type": type(post_id).__name__}
@app.get("/files/{file_path:path}") # Capture full path
def get_file(file_path: str):
return {"file_path": file_path}Multiple Path Parameters
Multiple Parameterspython
@app.get("/users/{user_id}/posts/{post_id}")
def get_user_post(user_id: str, post_id: int):
return {
"user_id": user_id,
"post_id": post_id
}
@app.get("/api/v{version}/users/{user_id}")
def get_user_versioned(version: int, user_id: str):
return {
"api_version": version,
"user_id": user_id
}Query Parameters
Handle URL query strings with default values and validation
Basic Query Parameters
Query Parameterspython
@app.get("/search")
def search(q: str, limit: int = 10, offset: int = 0):
return {
"query": q,
"limit": limit,
"offset": offset
}
# Example: /search?q=python&limit=20&offset=10Optional Parameters
Optional Parameterspython
from typing import Optional
@app.get("/items")
def list_items(
category: Optional[str] = None,
sort: str = "name",
ascending: bool = True,
tags: list[str] = []
):
return {
"category": category,
"sort": sort,
"ascending": ascending,
"tags": tags
}
# Example: /items?category=electronics&sort=price&ascending=false&tags=new&tags=saleRoute Organization
Organize your routes with routers and prefixes
Using Routers
Router Examplepython
from sufast import App, Router
# Create a router for user-related endpoints
user_router = Router()
@user_router.get("/")
def list_users():
return {"users": []}
@user_router.get("/{user_id}")
def get_user(user_id: str):
return {"user_id": user_id}
@user_router.post("/")
def create_user():
return {"message": "User created"}
# Main app
app = App()
# Include the router with a prefix
app.include_router(user_router, prefix="/users")
# Routes will be available at:
# GET /users/
# GET /users/{user_id}
# POST /users/Nested Routers
Nested Routerspython
from sufast import App, Router
# API v1 router
v1_router = Router()
@v1_router.get("/users")
def v1_users():
return {"version": "1.0", "users": []}
# API v2 router
v2_router = Router()
@v2_router.get("/users")
def v2_users():
return {"version": "2.0", "users": [], "pagination": {}}
# Main app
app = App()
# Include versioned routers
app.include_router(v1_router, prefix="/api/v1")
app.include_router(v2_router, prefix="/api/v2")
# Routes:
# GET /api/v1/users
# GET /api/v2/usersRoute Validation
Validate path and query parameters with custom constraints
Route Validationpython
from sufast import App
from typing import Annotated
app = App()
# Custom validation with constraints
@app.get("/users/{user_id}")
def get_user(user_id: Annotated[int, "User ID must be positive"] = None):
if user_id <= 0:
return {"error": "Invalid user ID"}, 400
return {"user_id": user_id}
@app.get("/search")
def search(
q: Annotated[str, "Query string (min 3 chars)"],
limit: Annotated[int, "Max 100"] = 10
):
if len(q) < 3:
return {"error": "Query too short"}, 400
if limit > 100:
return {"error": "Limit too high"}, 400
return {"query": q, "limit": limit}