Skip to content

Quickstart

Run an existing ASGI app with h2corn module:app, or start with the FastAPI example below.

Install

Requires CPython 3.11 or newer. See Installation and Python compatibility and wheels for optional extras such as h2corn[uvloop], runtime support, and platform details. Multiple workers require Unix; Windows runs one worker.

uv add h2corn fastapi
pip install h2corn fastapi

With uv, prefix the commands below with uv run.

Run your first server

Save as hello.py:

hello.py
from fastapi import FastAPI

app = FastAPI()


@app.get('/')
async def index():
    return {'message': 'hello from h2corn'}

Start the development server:

h2corn hello:app

By default h2corn binds 127.0.0.1:8000. Visiting http://127.0.0.1:8000/ returns {"message": "hello from h2corn"}.

Browsers use HTTP/1.1 here: they require TLS for HTTP/2.

Try HTTP/2

An HTTP/2 client must opt into cleartext prior knowledge. Use a curl build that lists HTTP/2 in curl --version:

curl --http2-prior-knowledge --fail http://127.0.0.1:8000/

This uses the same listener as the HTTP/1.1 request above.

Hot reload

While iterating locally, add --reload so h2corn restarts whenever your source files change:

h2corn hello:app --reload

The watcher follows *.py by default; tune it with --reload-include and --reload-exclude. Reload is intended for development only and cannot be combined with multiple workers.

Application factories

Pass --factory (or factory=True with serve / Server) when the target is a synchronous zero-argument factory. h2corn calls it once per request loop on startup and reload; acquire asynchronous resources in the returned app's lifespan. See Free-threaded Python for multi-loop factory behavior.

factory.py
from fastapi import FastAPI


def create_app() -> FastAPI:
    app = FastAPI()

    @app.get('/')
    async def index():
        return {'message': 'hello from h2corn'}

    return app
h2corn factory:create_app --factory

Next steps

  • Behind a proxy — nginx, Caddy, and HAProxy recipes with TLS at the edge and a private h2c upstream.
  • Direct TLS — terminate TLS in h2corn itself for single-server deployments.
  • Operations — multi-worker supervisor, signals, rolling reload, live scaling, and recycling.
  • Migrating — Uvicorn, Hypercorn, and Gunicorn commands and behavior differences.
  • Configuration — every option, in CLI, environment, and TOML form.