# Development

The first thing that should be mentioned is that the webserver that is integrated into Sanic is not just a development server.

It is production ready out-of-the-box, unless you enable in debug mode.

# Debug mode

By setting the debug mode, Sanic will be more verbose in its output and will disable several run-time optimizations.

# server.py
from sanic import Sanic
from sanic.response import json
app = Sanic(__name__)
async def hello_world(request):
    return json({"hello": "world"})
sanic server:app --host= --port=1234 --debug


Sanic's debug mode will slow down the server's performance and is therefore advised to enable it only in development environments.

# Automatic Reloader

Sanic offers a way to enable or disable the Automatic Reloader. The easiest way to enable it is using the CLI's --reload argument to activate the Automatic Reloader. Every time a Python file is changed, the reloader will restart your application automatically. This is very convenient while developing.

NOTE: The reloader is only available when using Sanic's worker manager. If you have disabled it using --single-process then the reloader will not be available to you.

sanic path.to:app --reload

You can also use the shorthand property

sanic path.to:app -r

If you have additional directories that you would like to automatically reload on file save (for example, a directory of HTML templates), you can add that using --reload-dir.

sanic path.to:app --reload --reload-dir=/path/to/templates

Or multiple directories, shown here using the shorthand properties

sanic path.to:app -r -R /path/to/one -R /path/to/two

# Best of both worlds

If you would like to be in debug mode and have the Automatic Reloader running, you can pass dev=True. This is equivalent to debug + auto reload.

Added in v22.3

sanic path.to:app --dev

You can also use the shorthand property

sanic path.to:app -d

# Automatic TLS certificate

When running in DEBUG mode, you can ask Sanic to handle setting up localhost temporary TLS certificates. This is helpful if you want to access your local development environment with https://.

This functionality is provided by either mkcert (opens new window) or trustme (opens new window). Both are good choices, but there are some differences. trustme is a Python library and can be installed into your environment with pip. This makes for easy envrionment handling, but it is not compatible when running a HTTP/3 server. mkcert might be a more involved installation process, but can install a local CA and make it easier to use.

You can choose which platform to use by setting config.LOCAL_CERT_CREATOR. When set to "auto", it will select either option, preferring mkcert if possible.

app.config.LOCAL_CERT_CREATOR = "auto"
app.config.LOCAL_CERT_CREATOR = "mkcert"
app.config.LOCAL_CERT_CREATOR = "trustme"

Automatic TLS can be enabled at Sanic server run time:

sanic path.to.server:app --auto-tls --debug


Localhost TLS certificates (like those generated by both mkcert and trustme) are NOT suitable for production environments. If you are not familiar with how to obtain a real TLS certificate, checkout the How to... section.

Added in v22.6

MIT Licensed
Copyright © 2018-present Sanic Community Organization

~ Made with ❤️ and ☕️ ~