LinuxCommandLibrary

fastapi

Serve web APIs

TLDR

Run a FastAPI app with automatic reload (for development)

$ fastapi run [path/to/file.py] --reload
copy

Serve your app in both development mode
$ fastapi dev [path/to/file.py]
copy

Specify the host and port to run on
$ fastapi run [path/to/file.py] --host [host_address] --port [port]
copy

Set the app variable name (if not app) or specify a custom app directory
$ fastapi run [path/to/file.py] --app-dir [path/to/app] --app [custom_app_name]
copy

Display help
$ fastapi --help
copy

Display help for a subcommand
$ fastapi [subcommand] --help
copy

SYNOPSIS

fastapi [OPTIONS] COMMAND [ARGS]...

Commands:
  dev      Run development server with auto-reload
  run      Run production server without reload

PARAMETERS

--host TEXT
    Host IP to bind. Default: 127.0.0.1

--port INTEGER
    Port to listen on. Default: 8000

--app-dir PATH
    Directory for app files. Default: .

--log-level {trace|debug|info|warning|error|critical|off}
    Set logging verbosity. Default: info

--reload
    Enable/disable auto-reload (dev only). Default: True

--workers INTEGER
    Number of worker processes. Default: 1

--app-name TEXT
    ASGI app variable name. Default: app

--env-file PATH
    Load variables from file

--customize PATH
    Python module with customize() function for Uvicorn tweaks

MAIN_MODULE_FILE
    Path to Python file with ASGI app (e.g., main.py)

DESCRIPTION

The fastapi command provides a convenient CLI interface for running FastAPI applications, a modern Python web framework for building APIs with ASGI. It simplifies server startup, especially during development, by wrapping Uvicorn with sensible defaults and auto-reload capabilities.

Key subcommands include dev for interactive development with file watching and hot reload, and run for stable production-like execution without reloads. Users specify a main module file (e.g., main.py) containing an ASGI app instance named app by default.

Common use cases involve quick prototyping: create a main.py with from fastapi import FastAPI; app = FastAPI(), then fastapi dev main.py to launch at http://127.0.0.1:8000 with interactive docs at /docs. Options allow customizing host, port, log levels, workers, and environment files.

This tool reduces boilerplate compared to direct uvicorn invocation, enhancing productivity for Python developers building high-performance APIs with automatic OpenAPI/Swagger UI generation.

CAVEATS

Python-based; install via pip install fastapi-cli. Not native to Linux. Subcommand options vary slightly (run ignores --reload). Requires FastAPI app structure.

INSTALLATION

pip install fastapi-cli
Optionally: pip install "fastapi[all]" for full features.

BASIC EXAMPLE

Create main.py:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root(): return {"Hello": "World"}

Run: fastapi dev main.py
Visit: http://127.0.0.1:8000/docs

HISTORY

FastAPI CLI debuted as fastapi-cli package in 2023, integrated into core FastAPI ~0.104. Evolved from manual uvicorn calls to provide streamlined dev/prod workflows, aligning with FastAPI's focus on developer experience since 2018.

SEE ALSO

uvicorn(1), gunicorn(1), pip(1)

Copied to clipboard