<summary>About the command <code>fastapi dev main.py</code>...</summary>
<summary>About the command <code>fastapi dev</code>...</summary>
The command `fastapi dev` reads your `main.py` file, detects the **FastAPI** app in it, and starts a server using [Uvicorn](https://www.uvicorn.dev).
The command `fastapi dev` reads your `main.py` file automatically, detects the **FastAPI** app in it, and starts a server using [Uvicorn](https://www.uvicorn.dev).
By default, `fastapi dev` will start with auto-reload enabled for local development.
@ -459,20 +459,6 @@ You can optionally deploy your FastAPI app to [FastAPI Cloud](https://fastapiclo
If you already have a **FastAPI Cloud** account (we invited you from the waiting list 😉), you can deploy your application with one command.
**FastAPI CLI** is a command line program that you can use to serve your FastAPI app, manage your FastAPI project, and more.
**FastAPI <abbrtitle="command line interface">CLI</abbr>** is a command line program that you can use to serve your FastAPI app, manage your FastAPI project, and more.
When you install FastAPI (e.g. with `pip install "fastapi[standard]"`), it includes a package called `fastapi-cli`, this package provides the `fastapi` command in the terminal.
When you install FastAPI (e.g. with `pip install "fastapi[standard]"`), it comes with a command line program you can run in the terminal.
To run your FastAPI app for development, you can use the `fastapi dev` command:
<spanstyle="background-color:#009485"><fontcolor="#D3D7CF"> FastAPI </font></span> Starting development server 🚀
@ -46,14 +46,67 @@ $ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid
</div>
The command line program called `fastapi` is **FastAPI CLI**.
/// tip
FastAPI CLI takes the path to your Python program (e.g. `main.py`) and automatically detects the `FastAPI` instance (commonly named `app`), determines the correct import process, and then serves it.
For production you would use `fastapi run` instead of `fastapi dev`. 🚀
For production you would use `fastapi run` instead. 🚀
The `fastapi` CLI will try to detect automatically the FastAPI app to run, assuming it's an object called `app` in a file `main.py` (or a couple other variants).
But you can configure explicitly the app to use.
## Configure the app `entrypoint` in `pyproject.toml` { #configure-the-app-entrypoint-in-pyproject-toml }
You can configure where your app is located in a `pyproject.toml` file like:
```toml
[tool.fastapi]
entrypoint = "main:app"
```
That `entrypoint` will tell the `fastapi` command that it should import the app like:
```python
from main import app
```
If your code was structured like:
```
.
├── backend
│ ├── main.py
│ ├── __init__.py
```
Then you would set the `entrypoint` as:
```toml
[tool.fastapi]
entrypoint = "backend.main:app"
```
which would be equivalent to:
```python
from backend.main import app
```
### `fastapi dev` with path { #fastapi-dev-with-path }
You can also pass the file path to the `fastapi dev` command, and it will guess the FastAPI app object to use:
```console
$ fastapi dev main.py
```
But you would have to remember to pass the correct path every time you call the `fastapi` command.
Additionally, other tools might not be able to find it, for example the [VS Code Extension](editor-support.md) or [FastAPI Cloud](https://fastapicloud.com), so it is recommended to use the `entrypoint` in `pyproject.toml`.
## `fastapi dev` { #fastapi-dev }
Running `fastapi dev` initiates development mode.
@ -62,7 +115,7 @@ By default, **auto-reload** is enabled, automatically reloading the server when
## `fastapi run` { #fastapi-run }
Executing `fastapi run` starts FastAPI in production mode by default.
Executing `fastapi run` starts FastAPI in production mode.
By default, **auto-reload** is disabled. It also listens on the IP address `0.0.0.0`, which means all the available IP addresses, this way it will be publicly accessible to anyone that can communicate with the machine. This is how you would normally run it in production, for example, in a container.
<summary>About the command <code>fastapi dev main.py</code>...</summary>
<summary>About the command <code>fastapi dev</code>...</summary>
The command `fastapi dev` reads your `main.py` file, detects the **FastAPI** app in it, and starts a server using [Uvicorn](https://www.uvicorn.dev).
The command `fastapi dev` reads your `main.py` file automatically, detects the **FastAPI** app in it, and starts a server using [Uvicorn](https://www.uvicorn.dev).
By default, `fastapi dev` will start with auto-reload enabled for local development.
@ -456,20 +456,6 @@ You can optionally deploy your FastAPI app to [FastAPI Cloud](https://fastapiclo
If you already have a **FastAPI Cloud** account (we invited you from the waiting list 😉), you can deploy your application with one command.
@ -465,6 +465,37 @@ As we cannot just isolate them and "mount" them independently of the rest, the *
///
## Configure the `entrypoint` in `pyproject.toml` { #configure-the-entrypoint-in-pyproject-toml }
As your FastAPI `app` object lives in `app/main.py`, you can configure the `entrypoint` in your `pyproject.toml` file like this:
```toml
[tool.fastapi]
entrypoint = "app.main:app"
```
that is equivalent to importing like:
```python
from app.main import app
```
That way the `fastapi` command will know where to find your app.
/// Note
You could also pass the path to the command, like:
```console
$ fastapi dev app/main.py
```
But you would have to remember to pass the correct path every time you call the `fastapi` command.
Additionally, other tools might not be able to find it, for example the [VS Code Extension](../editor-support.md) or [FastAPI Cloud](https://fastapicloud.com), so it is recommended to use the `entrypoint` in `pyproject.toml`.
///
## Check the automatic API docs { #check-the-automatic-api-docs }
Now, run your app:
@ -472,7 +503,7 @@ Now, run your app:
<divclass="termy">
```console
$ fastapi dev app/main.py
$ fastapi dev
<spanstyle="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
<spanstyle="background-color:#009485"><fontcolor="#D3D7CF"> FastAPI </font></span> Starting development server 🚀
@ -143,6 +143,55 @@ And there are dozens of alternatives, all based on OpenAPI. You could easily add
You could also use it to generate code automatically, for clients that communicate with your API. For example, frontend, mobile or IoT applications.
### Configure the app `entrypoint` in `pyproject.toml` { #configure-the-app-entrypoint-in-pyproject-toml }
You can configure where your app is located in a `pyproject.toml` file like:
```toml
[tool.fastapi]
entrypoint = "main:app"
```
That `entrypoint` will tell the `fastapi` command that it should import the app like:
```python
from main import app
```
If your code was structured like:
```
.
├── backend
│ ├── main.py
│ ├── __init__.py
```
Then you would set the `entrypoint` as:
```toml
[tool.fastapi]
entrypoint = "backend.main:app"
```
which would be equivalent to:
```python
from backend.main import app
```
### `fastapi dev` with path { #fastapi-dev-with-path }
You can also pass the file path to the `fastapi dev` command, and it will guess the FastAPI app object to use:
```console
$ fastapi dev main.py
```
But you would have to remember to pass the correct path every time you call the `fastapi` command.
Additionally, other tools might not be able to find it, for example the [VS Code Extension](../editor-support.md) or [FastAPI Cloud](https://fastapicloud.com), so it is recommended to use the `entrypoint` in `pyproject.toml`.
### Deploy your app (optional) { #deploy-your-app-optional }
You can optionally deploy your FastAPI app to [FastAPI Cloud](https://fastapicloud.com), go and join the waiting list if you haven't. 🚀