You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-26Lines changed: 44 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,18 +139,6 @@ $ pip install fastapi
139
139
140
140
</div>
141
141
142
-
You will also need an ASGI server, for production such as <ahref="https://www.uvicorn.org"class="external-link"target="_blank">Uvicorn</a> or <ahref="https://github.com/pgjones/hypercorn"class="external-link"target="_blank">Hypercorn</a>.
143
-
144
-
<divclass="termy">
145
-
146
-
```console
147
-
$ pip install "uvicorn[standard]"
148
-
149
-
---> 100%
150
-
```
151
-
152
-
</div>
153
-
154
142
## Example
155
143
156
144
### Create it
@@ -211,25 +199,38 @@ Run the server with:
211
199
<divclass="termy">
212
200
213
201
```console
214
-
$ uvicorn main:app --reload
215
-
202
+
$ fastapi dev main.py
203
+
204
+
╭────────── FastAPI CLI - Development mode ───────────╮
205
+
│ │
206
+
│ Serving at: http://127.0.0.1:8000 │
207
+
│ │
208
+
│ API docs: http://127.0.0.1:8000/docs │
209
+
│ │
210
+
│ Running in development mode, for production use: │
INFO: Will watch for changes in these directories: ['/home/user/code/awesomeapp']
216
217
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
217
-
INFO: Started reloader process [28720]
218
-
INFO: Started server process [28722]
218
+
INFO: Started reloader process [2248755] using WatchFiles
219
+
INFO: Started server process [2248757]
219
220
INFO: Waiting for application startup.
220
221
INFO: Application startup complete.
221
222
```
222
223
223
224
</div>
224
225
225
226
<detailsmarkdown="1">
226
-
<summary>About the command <code>uvicorn main:app --reload</code>...</summary>
227
+
<summary>About the command <code>fastapi dev main.py</code>...</summary>
227
228
228
-
The command `uvicorn main:app` refers to:
229
+
The command `fastapi dev` reads your `main.py` file, detects the **FastAPI** app in it, and starts a server using <ahref="https://www.uvicorn.org"class="external-link"target="_blank">Uvicorn</a>.
229
230
230
-
*`main`: the file `main.py` (the Python "module").
231
-
*`app`: the object created inside of `main.py` with the line `app = FastAPI()`.
232
-
*`--reload`: make the server restart after code changes. Only do this for development.
231
+
By default, `fastapi dev` will start with auto-reload enabled for local development.
232
+
233
+
You can read more about it in the <ahref="https://fastapi.tiangolo.com/fastapi-cli/"target="_blank">FastAPI CLI docs</a>.
The server should reload automatically (because you added `--reload` to the `uvicorn` command above).
306
+
The `fastapi dev`server should reload automatically.
306
307
307
308
### Interactive API docs upgrade
308
309
@@ -446,7 +447,7 @@ Independent TechEmpower benchmarks show **FastAPI** applications running under U
446
447
447
448
To understand more about it, see the section <ahref="https://fastapi.tiangolo.com/benchmarks/"class="internal-link"target="_blank">Benchmarks</a>.
448
449
449
-
## Optional Dependencies
450
+
## Dependencies
450
451
451
452
Used by Pydantic:
452
453
@@ -459,16 +460,33 @@ Used by Starlette:
459
460
* <ahref="https://www.python-httpx.org"target="_blank"><code>httpx</code></a> - Required if you want to use the `TestClient`.
460
461
* <ahref="https://jinja.palletsprojects.com"target="_blank"><code>jinja2</code></a> - Required if you want to use the default template configuration.
461
462
* <ahref="https://github.com/Kludex/python-multipart"target="_blank"><code>python-multipart</code></a> - Required if you want to support form <abbrtitle="converting the string that comes from an HTTP request into Python data">"parsing"</abbr>, with `request.form()`.
462
-
* <ahref="https://pythonhosted.org/itsdangerous/"target="_blank"><code>itsdangerous</code></a> - Required for `SessionMiddleware` support.
463
-
* <ahref="https://pyyaml.org/wiki/PyYAMLDocumentation"target="_blank"><code>pyyaml</code></a> - Required for Starlette's `SchemaGenerator` support (you probably don't need it with FastAPI).
464
463
465
464
Used by FastAPI / Starlette:
466
465
467
466
* <ahref="https://www.uvicorn.org"target="_blank"><code>uvicorn</code></a> - for the server that loads and serves your application.
468
467
* <ahref="https://github.com/ijl/orjson"target="_blank"><code>orjson</code></a> - Required if you want to use `ORJSONResponse`.
469
468
* <ahref="https://github.com/esnme/ultrajson"target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
469
+
*`fastapi-cli` - to provide the `fastapi` command.
470
+
471
+
When you install `fastapi` it comes these standard dependencies.
472
+
473
+
## `fastapi-slim`
474
+
475
+
If you don't want the extra standard optional dependencies, install `fastapi-slim` instead.
476
+
477
+
When you install with:
478
+
479
+
```bash
480
+
pip install fastapi
481
+
```
482
+
483
+
...it includes the same code and dependencies as:
484
+
485
+
```bash
486
+
pip install "fastapi-slim[standard]"
487
+
```
470
488
471
-
You can install all of these with `pip install "fastapi[all]"`.
489
+
The standard extra dependencies are the ones mentioned above.
And the proxy would be **"stripping"** the **path prefix** on the fly before transmitting the request to Uvicorn, keeping your application convinced that it is being served at `/app`, so that you don't have to update all your code to include the prefix `/api/v1`.
25
+
And the proxy would be **"stripping"** the **path prefix** on the fly before transmitting the request to the app server (probably Uvicorn via FastAPI CLI), keeping your application convinced that it is being served at `/app`, so that you don't have to update all your code to include the prefix `/api/v1`.
26
26
27
27
Up to here, everything would work as normally.
28
28
@@ -63,7 +63,7 @@ The docs UI would also need the OpenAPI schema to declare that this API `server`
63
63
}
64
64
```
65
65
66
-
In this example, the "Proxy" could be something like **Traefik**. And the server would be something like **Uvicorn**, running your FastAPI application.
66
+
In this example, the "Proxy" could be something like **Traefik**. And the server would be something like FastAPI CLI with **Uvicorn**, running your FastAPI application.
67
67
68
68
### Providing the `root_path`
69
69
@@ -72,7 +72,7 @@ To achieve this, you can use the command line option `--root-path` like:
72
72
<divclass="termy">
73
73
74
74
```console
75
-
$ uvicorn main:app --root-path /api/v1
75
+
$ fastapi run main.py --root-path /api/v1
76
76
77
77
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
78
78
```
@@ -101,7 +101,7 @@ Then, if you start Uvicorn with:
101
101
<divclass="termy">
102
102
103
103
```console
104
-
$ uvicorn main:app --root-path /api/v1
104
+
$ fastapi run main.py --root-path /api/v1
105
105
106
106
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
107
107
```
@@ -216,12 +216,12 @@ INFO[0000] Configuration loaded from file: /home/user/awesomeapi/traefik.toml
216
216
217
217
</div>
218
218
219
-
And now start your app with Uvicorn, using the `--root-path` option:
219
+
And now start your app, using the `--root-path` option:
220
220
221
221
<divclass="termy">
222
222
223
223
```console
224
-
$ uvicorn main:app --root-path /api/v1
224
+
$ fastapi run main.py --root-path /api/v1
225
225
226
226
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Copy file name to clipboardExpand all lines: docs/en/docs/advanced/openapi-callbacks.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -172,7 +172,7 @@ Now use the parameter `callbacks` in *your API's path operation decorator* to pa
172
172
173
173
### Check the docs
174
174
175
-
Now you can start your app with Uvicorn and go to <ahref="http://127.0.0.1:8000/docs"class="external-link"target="_blank">http://127.0.0.1:8000/docs</a>.
175
+
Now you can start your app and go to <ahref="http://127.0.0.1:8000/docs"class="external-link"target="_blank">http://127.0.0.1:8000/docs</a>.
176
176
177
177
You will see your docs including a "Callbacks" section for your *path operation* that shows how the *external API* should look like:
Copy file name to clipboardExpand all lines: docs/en/docs/advanced/openapi-webhooks.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ This is because it is expected that **your users** would define the actual **URL
44
44
45
45
### Check the docs
46
46
47
-
Now you can start your app with Uvicorn and go to <ahref="http://127.0.0.1:8000/docs"class="external-link"target="_blank">http://127.0.0.1:8000/docs</a>.
47
+
Now you can start your app and go to <ahref="http://127.0.0.1:8000/docs"class="external-link"target="_blank">http://127.0.0.1:8000/docs</a>.
48
48
49
49
You will see your docs have the normal *path operations* and now also some **webhooks**:
Copy file name to clipboardExpand all lines: docs/en/docs/advanced/wsgi.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Now, every request under the path `/v1/` will be handled by the Flask applicatio
22
22
23
23
And the rest will be handled by **FastAPI**.
24
24
25
-
If you run it with Uvicorn and go to <ahref="http://localhost:8000/v1/"class="external-link"target="_blank">http://localhost:8000/v1/</a> you will see the response from Flask:
25
+
If you run it and go to <ahref="http://localhost:8000/v1/"class="external-link"target="_blank">http://localhost:8000/v1/</a> you will see the response from Flask:
Copy file name to clipboardExpand all lines: docs/en/docs/deployment/concepts.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,7 @@ In most cases, when you create a web API, you want it to be **always running**,
94
94
95
95
### In a Remote Server
96
96
97
-
When you set up a remote server (a cloud server, a virtual machine, etc.) the simplest thing you can do is to run Uvicorn (or similar) manually, the same way you do when developing locally.
97
+
When you set up a remote server (a cloud server, a virtual machine, etc.) the simplest thing you can do is to use `fastapi run`, Uvicorn (or similar) manually, the same way you do when developing locally.
98
98
99
99
And it will work and will be useful **during development**.
So, it's important to put this **near the end** of the `Dockerfile`, to optimize the container image build times.
216
213
217
-
6. Set the **command** to run the `uvicorn` server.
214
+
6. Set the **command** to use `fastapi run`, which uses Uvicorn underneath.
218
215
219
216
`CMD` takes a list of strings, each of these strings is what you would type in the command line separated by spaces.
220
217
221
218
This command will be run from the **current working directory**, the same `/code` directory you set above with `WORKDIR /code`.
222
219
223
-
Because the program will be started at `/code` and inside of it is the directory `./app` with your code, **Uvicorn** will be able to see and **import**`app` from `app.main`.
224
-
225
220
!!! tip
226
221
Review what each line does by clicking each number bubble in the code. 👆
227
222
@@ -238,10 +233,10 @@ You should now have a directory structure like:
238
233
239
234
#### Behind a TLS Termination Proxy
240
235
241
-
If you are running your container behind a TLS Termination Proxy (load balancer) like Nginx or Traefik, add the option `--proxy-headers`, this will tell Uvicorn to trust the headers sent by that proxy telling it that the application is running behind HTTPS, etc.
236
+
If you are running your container behind a TLS Termination Proxy (load balancer) like Nginx or Traefik, add the option `--proxy-headers`, this will tell Uvicorn (through the FastAPI CLI) to trust the headers sent by that proxy telling it that the application is running behind HTTPS, etc.
1. Copy the `main.py` file to the `/code` directory directly (without any `./app` directory).
369
364
370
-
2.Run Uvicorn and tell it to import the `app` object from `main` (instead of importing from `app.main`).
365
+
2.Use `fastapi run`to serve your application in the single file `main.py`.
371
366
372
-
Then adjust the Uvicorn command to use the new module `main` instead of `app.main`to import the FastAPI object `app`.
367
+
When you pass the file to `fastapi run` it will detect automatically that it is a single file and not part of a package and will know how to import it and serve your FastAPI app. 😎
10. Copy the `app` directory to the `/code` directory.
657
652
658
-
11.Run the `uvicorn` command, telling it to use the `app` object imported from `app.main`.
653
+
11.Use the `fastapi run` commandto run your app.
659
654
660
655
!!! tip
661
656
Click the bubble numbers to see what each line does.
@@ -677,7 +672,7 @@ Then in the next (and final) stage you would build the image more or less in the
677
672
Again, if you are running your container behind a TLS Termination Proxy (load balancer) like Nginx or Traefik, add the option `--proxy-headers` to the command:
0 commit comments