Source: https://h2corn.monicz.dev/quickstart/

# Quickstart

Run an existing ASGI app with `h2corn module:app`, or start with the [FastAPI](https://fastapi.tiangolo.com/) example below.

## Install

Requires **CPython 3.11 or newer**. See [Installation](https://h2corn.monicz.dev/installation/) and [Python compatibility and wheels](https://h2corn.monicz.dev/about/compatibility/) for optional extras such as `h2corn[uvloop]`, runtime support, and platform details. Multiple workers require Unix; Windows runs one worker.

**uv**

```bash
uv add h2corn fastapi
```

**pip**

```bash
pip install h2corn fastapi
```

With uv, prefix the commands below with `uv run`.

## Run your first server

Save as `hello.py`:

hello.py

```python
from fastapi import FastAPI

app = FastAPI()


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

Start the development server:

```bash
h2corn hello:app
```

By default h2corn binds [`127.0.0.1:8000`](https://h2corn.monicz.dev/configuration/#option-bind). 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`:

```bash
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`](https://h2corn.monicz.dev/configuration/#command-reload) so h2corn restarts whenever your source files change:

```bash
h2corn hello:app --reload
```

The watcher follows `*.py` by default; tune it with [`--reload-include`](https://h2corn.monicz.dev/configuration/#command-reload_include) and [`--reload-exclude`](https://h2corn.monicz.dev/configuration/#command-reload_exclude). Reload is intended for development only and cannot be combined with multiple workers.

## Application factories

Pass [`--factory`](https://h2corn.monicz.dev/configuration/#command-factory) (or `factory=True` with [`serve`](https://h2corn.monicz.dev/api/server/) / [`Server`](https://h2corn.monicz.dev/api/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](https://h2corn.monicz.dev/guide/operations/#free-threaded-python) for multi-loop factory behavior.

factory.py

```python
from fastapi import FastAPI


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

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

    return app
```

```bash
h2corn factory:create_app --factory
```

## Next steps

- [Behind a proxy](https://h2corn.monicz.dev/guide/proxy/#behind-a-reverse-proxy) — nginx, Caddy, and HAProxy recipes with TLS at the edge and a private `h2c` upstream.
- [Direct TLS](https://h2corn.monicz.dev/guide/tls/#direct-tls) — terminate TLS in `h2corn` itself for single-server deployments.
- [Operations](https://h2corn.monicz.dev/guide/operations/) — multi-worker supervisor, signals, rolling reload, live scaling, and recycling.
- [Migrating](https://h2corn.monicz.dev/migrating/) — Uvicorn, Hypercorn, and Gunicorn commands and behavior differences.
- [Configuration](https://h2corn.monicz.dev/configuration/#option-index) — every option, in CLI, environment, and TOML form.
