Skip to content

Examples

Real-world examples and patterns for building APIs with REROUTE.

Framework-Specific Examples

Complete end-to-end examples for each supported framework:

Basic Examples

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:

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.