Examples¶
Real-world examples and patterns for building APIs with REROUTE.
Framework-Specific Examples¶
Complete end-to-end examples for each supported framework:
-
Full Flask application with Spectree OpenAPI, validation, and best practices
-
Async FastAPI application with background tasks, caching, and deployment guide
Basic Examples¶
-
Create, Read, Update, Delete operations
-
User authentication and authorization
-
Protect your API with rate limits
-
Improve performance with caching
-
Connect to databases with SQLAlchemy, Tortoise ORM, and more
Patterns¶
Simple API¶
from reroute import RouteBase
class UserRoutes(RouteBase):
def __init__(self):
super().__init__()
self.users = []
def get(self):
return {"users": self.users}
def post(self):
user = {"id": len(self.users) + 1}
self.users.append(user)
return user
With Decorators¶
from reroute import RouteBase
from reroute.decorators import rate_limit, cache
class ProductRoutes(RouteBase):
@cache(duration=300)
def get(self):
# Cached for 5 minutes
return {"products": [...]}
@rate_limit("10/min")
def post(self):
# Max 10 requests per minute
return {"created": True}
With Lifecycle Hooks¶
from reroute import RouteBase
class SecureRoutes(RouteBase):
def before_request(self):
# Check authentication
if not self.is_authenticated():
raise Unauthorized("Login required")
return None
def after_request(self, response):
# Add security headers
response["X-API-Version"] = "1.0"
return response
def on_error(self, error):
# Custom error handling
return {"error": str(error), "status": 500}
Full Examples¶
Browse complete example applications:
- Basic CRUD - User management API
- Authentication - JWT authentication
- Rate Limiting - API rate limiting patterns
- Caching - Response caching strategies
- Database Integration - SQL and ORM integration (SQLAlchemy, Tortoise ORM)
Example Repository¶
Check out the examples folder in the main repository for runnable examples.
Community Examples¶
Share your examples with the community! Submit a PR to add your example to this page.