This is a reference application that you can use to get your next Litestar application running quickly.
It contains most of the boilerplate required for a production web API with features like:
- Latest Litestar configured with best practices
- Integration with SQLAlchemy 2.0, SAQ (Simple Asynchronous Queue), Structlog, and Granian
- Extends built-in Litestar click CLI
- Frontend integrated with Vite and includes Jinja2 templates that integrate with Vite websocket/HMR support
- Multi-stage Docker build using a minimal Python 3.12 runtime image.
- Optional Multi-stage Distroless Docker build.
- Pre-configured user model that includes teams and associated team roles
- Examples of using guards for superuser and team-based auth.
- Examples using raw SQL for more complex queries
Take what you need and adapt it to your own projects
To quickly get a development environment running, run the following:
make install
. .venv/bin/activate
cp .env.local.example .env
make start-infra # this starts a database and redis instance only
# this will start the SAQ worker, Vite development process, and Litestar
uv run app run
# to stop the database and redis, run
make stop-infra
docker compose up
We have documented the process to help you get the repository up and running. Check out the documentation for more information.
Command Examples
โฏ app
Usage: app [OPTIONS] COMMAND [ARGS]...
Litestar CLI.
โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ --app TEXT Module path to a Litestar application (TEXT) โ
โ --app-dir DIRECTORY Look for APP in the specified directory, by adding โ
โ this to the PYTHONPATH. Defaults to the current โ
โ working directory. โ
โ (DIRECTORY) โ
โ --help -h Show this message and exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Using Litestar app from env: 'app.asgi:app'
Loading environment configuration from .env
โญโ Commands โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ assets Manage Vite Tasks. โ
โ database Manage SQLAlchemy database components. โ
โ info Show information about the detected Litestar app. โ
โ routes Display information about the application's routes. โ
โ run Run a Litestar app. โ
โ schema Manage server-side OpenAPI schemas. โ
โ sessions Manage server-side sessions. โ
โ users Manage application users and roles. โ
โ version Show the currently installed Litestar version. โ
โ workers Manage background task workers. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Alembic integration is built directly into the CLI under the database
command.
โฏ app database
Using Litestar app from env: 'app.asgi:create_app'
Usage: app database [OPTIONS] COMMAND [ARGS]...
Manage SQLAlchemy database components.
โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ --help -h Show this message and exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโ Commands โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ downgrade Downgrade database to a specific revision. โ
โ init Initialize migrations for the project. โ
โ make-migrations Create a new migration revision. โ
โ merge-migrations Merge multiple revisions into a single new revision. โ
โ show-current-revision Shows the current revision for the database. โ
โ stamp-migration Mark (Stamp) a specific revision as current without โ
โ applying the migrations. โ
โ upgrade Upgrade database to a specific revision. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฏ app database upgrade
Using Litestar app from env: 'app.asgi:create_app'
Starting database upgrade process โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Are you sure you you want migrate the database to the "head" revision? [y/n]: y
2023-10-01T19:44:13.536101Z [debug ] Using selector: EpollSelector
2023-10-01T19:44:13.623437Z [info ] Context impl PostgresqlImpl.
2023-10-01T19:44:13.623617Z [info ] Will assume transactional DDL.
2023-10-01T19:44:13.667920Z [info ] Running upgrade -> c3a9a11cc35d, init
2023-10-01T19:44:13.774932Z [debug ] new branch insert c3a9a11cc35d
2023-10-01T19:44:13.783804Z [info ] Pool disposed. Pool size: 5 Connections
in pool: 0 Current Overflow: -5 Current Checked out connections: 0
2023-10-01T19:44:13.784013Z [info ] Pool recreating
โฏ app worker
Using Litestar app from env: 'app.asgi:create_app'
Usage: app worker [OPTIONS] COMMAND [ARGS]...
Manage application background workers.
โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ --help -h Show this message and exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโ Commands โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ run Starts the background workers. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
To run the application through Granian (HTTP1 or HTTP2) using the standard Litestar CLI, you can use the following:
โฏ app run --help
Using Litestar app from env: 'app.asgi:app'
Loading environment configuration from .env
Usage: app run [OPTIONS]
Run a Litestar app.
The app can be either passed as a module path in the form of <module
name>.<submodule>:<app instance or factory>, set as an environment variable
LITESTAR_APP with the same format or automatically discovered from one of
these canonical paths: app.py, asgi.py, application.py or app/__init__.py.
When auto-discovering application factories, functions with the name
``create_app`` are considered, or functions that are annotated as returning a
``Litestar`` instance.
โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ --port -p INTEGER Serve under this port โ
โ (INTEGER) โ
โ [default: 8000] โ
โ --wc,--web-concurrencyโฆ -W INTEGER RANGE The number of processes โ
โ [1<=x<=7] to start. โ
โ (INTEGER RANGE) โ
โ [default: 1; 1<=x<=7] โ
โ --threads INTEGER RANGE [x>=1] The number of threads. โ
โ (INTEGER RANGE) โ
โ [default: 1; x>=1] โ
โ --blocking-threads INTEGER RANGE [x>=1] The number of blocking โ
โ threads. โ
โ (INTEGER RANGE) โ
โ [default: 1; x>=1] โ
โ --threading-mode THREADMODES Threading mode to use. โ
โ (THREADMODES) โ
โ --http HTTPMODES HTTP Version to use โ
โ (HTTP or HTTP2) โ
โ (HTTPMODES) โ
โ --opt Enable additional event โ
โ loop optimizations โ
โ --backlog INTEGER RANGE [x>=128] Maximum number of โ
โ connections to hold in โ
โ backlog. โ
โ (INTEGER RANGE) โ
โ [default: 1024; x>=128] โ
โ --host -H TEXT Server under this host โ
โ (TEXT) โ
โ [default: 127.0.0.1] โ
โ --ssl-keyfile FILE SSL key file (FILE) โ
โ --ssl-certificate FILE SSL certificate file โ
โ (FILE) โ
โ --create-self-signed-cโฆ If certificate and key โ
โ are not found at โ
โ specified locations, โ
โ create a self-signed โ
โ certificate and a key โ
โ --http1-buffer-size INTEGER RANGE Set the maximum buffer โ
โ [x>=8192] size for HTTP/1 โ
โ connections โ
โ (INTEGER RANGE) โ
โ [default: 417792; โ
โ x>=8192] โ
โ --http1-keep-alive/--nโฆ Enables or disables โ
โ HTTP/1 keep-alive โ
โ [default: โ
โ http1-keep-alive] โ
โ --http1-pipeline-flushโฆ Aggregates HTTP/1 โ
โ flushes to better โ
โ support pipelined โ
โ responses โ
โ (experimental) โ
โ --http2-adaptive-windoโฆ Sets whether to use an โ
โ adaptive flow control โ
โ for HTTP2 โ
โ --http2-initial-connecโฆ INTEGER Sets the max โ
โ connection-level flow โ
โ control for HTTP2 โ
โ (INTEGER) โ
โ --http2-initial-streamโฆ INTEGER Sets the โ
โ `SETTINGS_INITIAL_WINDโฆ โ
โ option for HTTP2 โ
โ stream-level flow โ
โ control โ
โ (INTEGER) โ
โ --http2-keep-alive-intโฆ OPTIONAL Sets an interval for โ
โ HTTP2 Ping frames โ
โ should be sent to keep โ
โ a connection alive โ
โ (OPTIONAL) โ
โ --http2-keep-alive-timโฆ INTEGER Sets a timeout for โ
โ receiving an โ
โ acknowledgement of the โ
โ HTTP2 keep-alive ping โ
โ (INTEGER) โ
โ --http2-max-concurrentโฆ INTEGER Sets the โ
โ SETTINGS_MAX_CONCURRENโฆ โ
โ option for HTTP2 โ
โ connections โ
โ (INTEGER) โ
โ --http2-max-frame-size INTEGER Sets the maximum frame โ
โ size to use for HTTP2 โ
โ (INTEGER) โ
โ --http2-max-headers-siโฆ INTEGER Sets the max size of โ
โ received header frames โ
โ (INTEGER) โ
โ --http2-max-send-buffeโฆ INTEGER Set the maximum write โ
โ buffer size for each โ
โ HTTP/2 stream โ
โ (INTEGER) โ
โ --url-path-prefix TEXT URL path prefix the app โ
โ is mounted on โ
โ (TEXT) โ
โ --debug -d Run app in debug mode โ
โ --pdb,--use-pdb -P Drop into PDB on an โ
โ exception โ
โ --respawn-failed-workeโฆ Enable workers respawn โ
โ on unexpected exit โ
โ --reload -r Reload server on โ
โ changes โ
โ --help -h Show this message and โ
โ exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ