Sanic Extensions

25.12.0 · active · verified Sat Apr 11

Sanic-Ext is a powerful extension package for the Sanic web framework, providing core functionality like OpenAPI documentation (Swagger UI, Redoc), dependency injection, request data validation, background tasks, and CORS handling. It aligns its major version numbers with Sanic releases (e.g., v25.12.0 for Sanic 23.12/24.12/25.12). The project releases new major versions annually, typically in December, with patch and minor updates in between.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Sanic-Ext with a Sanic application and use its dependency injection feature for route handlers. By type-hinting parameters in a route function, Sanic-Ext automatically extracts them from the request (e.g., query parameters, path parameters, or request body) and performs basic validation. After running, you can typically access OpenAPI documentation at `/docs` or `/swagger` in your browser.

from sanic import Sanic
from sanic_ext import Extend
from typing import Optional

app = Sanic("My App with Sanic-Ext")
Extend(app)

@app.get("/")
async def hello_world(name: str = "World"):
    # Sanic-Ext automatically injects query parameters based on type hints
    return {"hello": name}

@app.post("/items")
async def create_item(item_id: int, description: Optional[str] = None):
    # This demonstrates basic type-hinted injection for path/query parameters
    return {"item_id": item_id, "description": description, "status": "created"}

if __name__ == "__main__":
    # Run the Sanic application
    # Sanic-Ext provides features like OpenAPI docs at /docs or /swagger
    app.run(host="0.0.0.0", port=8000, debug=True)

view raw JSON →