Running handlers from Click

Recently, there was a question on the Discord server about integrating Sanic handlers with Click.

Here is a rough estimation of what it could look like.

import asyncio

import click

import server  # noqa
from handlers import handler

@click.option("--name", prompt="Your name", help="The person to greet.")
def hello(name):
    response =, name))

if __name__ == "__main__":
from sanic import Sanic, text

app = Sanic.get_app()

async def handler(request, name):
    return text(f"Hello {name}")
from sanic import Sanic

def create_app():
    app = Sanic(__name__)
    import handlers  # noqa

    return app

app = create_app()

With a setup like this, you could run this to get the server going:

sanic server:app

Or, this to hit the handler from CLI:


The important thing is to make sure that is no in the global scope anywhere that will run.