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

uvicorn [OPTIONS] APP_MODULE:APP_VARIABLE
Example: uvicorn main:app --reload --port 8000

PARAMETERS

APP_MODULE:APP_VARIABLE
    Specifies the Python module and the ASGI application instance to run (e.g., main:app where main.py contains app = FastAPI(...)).

--host


    Binds the server to a specific IP address (default: 127.0.0.1). Use 0.0.0.0 for external access.

--port
    Specifies the port to listen on (default: 8000).

--reload
    Enables auto-reloading of the server when code changes are detected. Ideal for development.

--workers
    Specifies the number of worker processes to run for production (often used with Gunicorn).

--log-level
    Sets the logging verbosity (e.g., info, debug, warning, error).

--proxy-headers
    Enables support for X-Forwarded-For and X-Forwarded-Proto headers when running behind a proxy.

--ssl-keyfile
    Path to the SSL key file for HTTPS.

--ssl-certfile
    Path to the SSL certificate file for HTTPS.

DESCRIPTION

While fastapi itself is not a standalone Linux command, it refers to the FastAPI Python web framework. To 'run' a FastAPI application on Linux, you typically use an ASGI (Asynchronous Server Gateway Interface) server. The most common and recommended server for this purpose is Uvicorn.

Uvicorn is a lightning-fast ASGI server built on uvloop and httptools. It enables serving asynchronous Python web applications, including those built with FastAPI, providing high performance and concurrency suitable for modern web services. When you hear 'running fastapi' on Linux, it almost invariably implies using the uvicorn command to serve your FastAPI application.

CAVEATS

FastAPI is a Python Framework, Not a Command: The name 'fastapi' refers to a Python web framework, not an executable binary on Linux. The actual command used to run a FastAPI application is usually uvicorn (or another ASGI server like Hypercorn).

Python Environment Required: To run FastAPI applications, you must have Python and pip installed, and the FastAPI and Uvicorn packages installed within a Python environment (ideally a virtual environment).

Development vs. Production: The --reload option is for development only. For production deployments, Uvicorn is often run behind a process manager like Gunicorn or directly by a system service (e.g., systemd), and usually fronted by a reverse proxy like Nginx or Caddy.

VIRTUAL ENVIRONMENTS

Using Python virtual environments (venv or conda) is highly recommended for FastAPI projects. This isolates project dependencies, preventing conflicts with other Python projects or the system's global Python installation.

DEPLOYMENT STRATEGIES

For production, running Uvicorn directly with --reload is not advised. Common strategies involve:

  • Using Gunicorn to manage multiple Uvicorn workers (e.g., gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app).
  • Deploying with containerization technologies like Docker.
  • Setting up a reverse proxy (Nginx, Caddy) to handle SSL termination, load balancing, and serve static files, forwarding dynamic requests to the Uvicorn server.

HISTORY

FastAPI was created by Sebastián Ramírez (tiangolo) and first released in December 2018. It quickly gained popularity due to its high performance (comparable to NodeJS and Go), automatic interactive API documentation (Swagger UI and ReDoc), and its strong foundation on standard Python type hints. Uvicorn, the recommended server, emerged as a performant ASGI server designed to run modern asynchronous Python web frameworks. Its development paralleled the rise of ASGI, enabling Python to compete effectively in the asynchronous web services space, which FastAPI leveraged fully.

SEE ALSO

uvicorn(1), python(1), pip(1), gunicorn(1), nginx(8)

Copied to clipboard