new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

FastAPI · Reference · all subjects

out of scope

11 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Recommended way to configure entrypoint

It is recommended to configure the entrypoint in pyproject.toml rather than using CLI options each time, because other tools like the VS Code Extension and FastAPI Cloud can discover and use the configuration in pyproject.toml.

FASTAPI_ENV environment variable behavior

The fastapi dev command sets the FASTAPI_ENV environment variable to 'development' before importing your app, unless it is already set. The conventional values for FASTAPI_ENV are 'development' and 'production'. The fastapi run command leaves FASTAPI_ENV unchanged, so you must set it explicitly if your app needs to detect production mode.

FastAPI CLI is a command line interface

FastAPI CLI is a command line program used to deploy FastAPI applications, manage FastAPI projects, and perform related tasks. It is installed when you add FastAPI to your project.

fastapi dev command for development

The fastapi dev command starts a development server for your FastAPI application. It automatically enables autoreload, which reloads the server when you make code changes. It listens on 127.0.0.1 (localhost only). Before importing the app, it sets the environment variable FASTAPI_ENV to 'development', unless it is already set.

fastapi run command for production

The fastapi run command starts FastAPI in production mode. Autoreload is disabled by default. It listens on 0.0.0.0, which means all available IP addresses, making it publicly accessible. This is the standard way to run FastAPI in production environments such as containers.

Configure app entrypoint in pyproject.toml

You can configure the FastAPI app entrypoint in a pyproject.toml file using the [tool.fastapi] section. The entrypoint setting specifies how to import the app, using the format 'module:app'. For example, entrypoint = 'main:app' tells FastAPI to execute 'from main import app'. For nested modules like backend/main.py, use 'backend.main:app' which translates to 'from backend.main import app'.

Specify app entrypoint via CLI option

You can pass the app entrypoint directly to fastapi dev using the --entrypoint option. For example: 'uv run fastapi dev --entrypoint main:app'. You can also pass a file path to have FastAPI detect the app object automatically, for example: 'uv run fastapi dev main.py'.

FastAPI CLI uses Uvicorn internally

The FastAPI CLI internally uses Uvicorn, which is a powerful, production-ready ASGI server.

Autoreload in development vs production

Autoreload is enabled by default in fastapi dev for development. It is resource-intensive and less stable than when disabled, so it should only be used for development. Autoreload is disabled by default in fastapi run for production.

Default app detection by FastAPI CLI

The FastAPI CLI automatically attempts to detect the FastAPI app to run. It assumes the app is an object named 'app' in a file named 'main.py' or similar variants. However, you can explicitly configure which app to use.

HTTPS and terminating proxy for production

In most cases, you should have a terminating proxy that manages HTTPS for you in production. This depends on how you deploy your application. Your provider may handle this for you, or you may need to set it up yourself.

Give your agent this brain