API Reference

Sanic

A fast Python web framework and server built for async, with simple routing and WebSocket support.

Official Documentation ↗
CompatibilityThis documentation covers version v25.12.1. Check the official docs for the latest API surface.

Installation

pippip install sanic

Methods

Sanic is a single-framework-and-server package where handlers are async functions.

Key Features

  • Async handlers and built-in server
  • Class-based views and blueprints
  • WebSocket and streaming responses

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

Errors

The official API reference declares the following error types:

  • sanic.exceptions.SanicException (exception(Sanic.get_app 取不存在的 app 时默认抛出)) — official text: “If you call Sanic.get_app("non-existing") on an app that does not exist, it will raise sanic.exceptions.SanicException by default.” (source: https://sanic.dev/en/guide/basics/app.html, fetched 2026-11-08T19:58:23+08:00)

API reference evidence

Fetched from official sources on 2026-11-08T19:58:23+08:00 via WebFetch https://sanic.dev/en/guide/basics/app.html;WebFetch https://sanic.dev/api/sanic.app.html;WebFetch https://sanic.dev/en/guide/getting-started.html.

  • Sanic → signature from https://sanic.dev/api/sanic.app.html; official text: “The main application instance class Sanic(name: str, config: Optional[config_type] = None, ctx: Optional[ctx_type] = None, router: Optional[Router] = None, signal_router: Optional[SignalRouter] = None, error_handler: Opt”
  • @app.get → signature from https://sanic.dev/en/guide/getting-started.html; official text: “from sanic import Sanic from sanic.response import text app = Sanic(“MyHelloWorldApp”) @app.get(“/”) async def hello_world(request): return text(“Hello, world.”)”

FAQ

Do I need a separate server?
No. Sanic includes a fast server, though for production you may still front it with a reverse proxy for TLS.
How do I share state?
Use app.ctx for application-wide objects; it is available on every request via request.app.ctx.
Why is my handler blocking?
A synchronous call inside an async handler blocks the loop. Offload CPU or blocking IO to a thread pool executor.