FastAPI CLI
FastAPI CLI (Command Line Interface) is a command-line program that simplifies running and managing FastAPI applications. It provides commands to serve apps in development and production modes, manage projects, and more. It is usually installed as part of `fastapi[standard]` and receives frequent updates, typically involving dependency bumps and occasional breaking changes related to Python version support or internal dependencies.
Warnings
- breaking Dropped support for Python 3.9 in version 0.0.23, and Python 3.8 in version 0.0.19. Ensure your project's Python version meets the minimum requirement (>=3.10).
- breaking Dropped support for Pydantic v1 in version 0.0.17. This may cause issues if your project relies on Pydantic v1. Note that version 0.0.16 had temporarily fixed Pydantic v1 support, but it was subsequently dropped again.
- deprecated The `fastapi-cli-slim` package was deprecated in version 0.0.21. Projects should now directly use `fastapi-cli`.
- gotcha Always use `fastapi dev` for local development and `fastapi run` for production-like environments. `fastapi dev` enables auto-reload and binds to `127.0.0.1` (localhost), while `fastapi run` disables auto-reload and binds to `0.0.0.0` (accessible externally), which is suitable for containers or behind a reverse proxy. Using `fastapi dev` in production is resource-intensive and less stable.
- gotcha For complex project structures or when other tools need to find your app, it's highly recommended to configure the `entrypoint` in your `pyproject.toml` file rather than always passing the file path to `fastapi dev` or `fastapi run`.
Install
-
pip install "fastapi[standard]" -
pip install fastapi-cli
Imports
- fastapi
This is a command-line utility, not a Python library meant for direct import into Python code. You invoke it from your terminal.
Quickstart
# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Hello FastAPI CLI!"}
# Terminal commands
# Run in development mode (with auto-reload):
# fastapi dev main.py
# Or, for explicit configuration (recommended for larger projects):
# 1. Add to pyproject.toml:
# [tool.fastapi]
# entrypoint = "main:app"
# 2. Then run:
# fastapi dev