FastAPI CLI

raw JSON →
0.0.24 verified Tue May 12 auth: no python install: stale quickstart: verified

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.

pip install "fastapi[standard]"
error fastapi: command not found
cause The 'fastapi' command-line interface (CLI) is not installed in the active Python environment, the virtual environment is not activated, or its installation path is not included in the system's PATH.
fix
Ensure FastAPI and its standard dependencies, which include fastapi-cli, are installed in your active virtual environment using pip install "fastapi[standard]" or pip install fastapi-cli.
error ModuleNotFoundError: No module named 'fastapi'
cause The FastAPI library is not installed in the currently active Python environment, or the virtual environment where it is installed is not activated.
fix
Activate your Python virtual environment (if using one) and install FastAPI using pip install fastapi or pip install "fastapi[standard]".
error ERROR: Error loading ASGI app. Could not import module "main".
cause The `fastapi-cli` (or `uvicorn` which it uses internally) cannot find the FastAPI application instance, typically named `app`, within the specified Python module or file (e.g., `main.py`) or the module path is incorrect.
fix
Verify that your FastAPI application instance is named app (e.g., app = FastAPI()) in the main file (e.g., main.py), and ensure you are running the fastapi dev or fastapi run command from the correct directory, or explicitly specify the module and app instance (e.g., fastapi dev my_module:my_app).
error Cannot import name 'FastApi' from 'fastapi'
cause This error is typically due to a capitalization mistake; the FastAPI class name is case-sensitive and should be `FastAPI` (with an uppercase 'API'). Additionally, naming your own Python script `fastapi.py` can cause an import conflict.
fix
Correct the import statement to from fastapi import FastAPI. Also, ensure your application's Python file is not named fastapi.py to avoid shadowing the library.
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).
fix Upgrade your Python environment to 3.10 or newer.
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.
fix Upgrade Pydantic to v2 and migrate your models if necessary, or pin `fastapi-cli` to an older version (e.g., <=0.0.16) if Pydantic v1 compatibility is critical.
deprecated The `fastapi-cli-slim` package was deprecated in version 0.0.21. Projects should now directly use `fastapi-cli`.
fix Migrate from `fastapi-cli-slim` to `fastapi-cli` if you were using the slim version.
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.
fix Use `fastapi run` for deploying your application. For robust production deployments, consider using a process manager like Gunicorn with Uvicorn, and a reverse proxy like Nginx.
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`.
fix Add a `[tool.fastapi]` section to your `pyproject.toml` with `entrypoint = "your_module:your_app_instance"` (e.g., `entrypoint = "main:app"`).
pip install fastapi-cli
python os / libc variant status wheel install import disk
3.10 alpine (musl) standard wheel - - 80.4M
3.10 alpine (musl) fastapi-cli wheel - - 51.2M
3.10 alpine (musl) standard - - - -
3.10 alpine (musl) fastapi-cli - - - -
3.10 slim (glibc) standard wheel 9.4s - 83M
3.10 slim (glibc) fastapi-cli wheel 4.5s - 56M
3.10 slim (glibc) standard - - - -
3.10 slim (glibc) fastapi-cli - - - -
3.11 alpine (musl) standard wheel - - 88.2M
3.11 alpine (musl) fastapi-cli wheel - - 55.5M
3.11 alpine (musl) standard - - - -
3.11 alpine (musl) fastapi-cli - - - -
3.11 slim (glibc) standard wheel 8.5s - 91M
3.11 slim (glibc) fastapi-cli wheel 4.3s - 60M
3.11 slim (glibc) standard - - - -
3.11 slim (glibc) fastapi-cli - - - -
3.12 alpine (musl) standard wheel - - 80.9M
3.12 alpine (musl) fastapi-cli wheel - - 49.0M
3.12 alpine (musl) standard - - - -
3.12 alpine (musl) fastapi-cli - - - -
3.12 slim (glibc) standard wheel 7.3s - 85M
3.12 slim (glibc) fastapi-cli wheel 4.0s - 54M
3.12 slim (glibc) standard - - - -
3.12 slim (glibc) fastapi-cli - - - -
3.13 alpine (musl) standard wheel - - 80.5M
3.13 alpine (musl) fastapi-cli wheel - - 48.6M
3.13 alpine (musl) standard - - - -
3.13 alpine (musl) fastapi-cli - - - -
3.13 slim (glibc) standard wheel 7.4s - 84M
3.13 slim (glibc) fastapi-cli wheel 3.9s - 54M
3.13 slim (glibc) standard - - - -
3.13 slim (glibc) fastapi-cli - - - -
3.9 alpine (musl) standard wheel - - 78.9M
3.9 alpine (musl) fastapi-cli wheel - - 50.1M
3.9 alpine (musl) standard - - - -
3.9 alpine (musl) fastapi-cli - - - -
3.9 slim (glibc) standard wheel 10.8s - 82M
3.9 slim (glibc) fastapi-cli wheel 5.3s - 54M
3.9 slim (glibc) standard - - - -
3.9 slim (glibc) fastapi-cli - - - -

Create a `main.py` file with a basic FastAPI application instance named `app`. Then, run the application using `fastapi dev main.py` for development with auto-reload. For more explicit project setup, configure the `entrypoint` in `pyproject.toml` and then simply run `fastapi dev`.

# 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