Then, declare your endpoint functions with `async def` like:
-```Python
+```Python hl_lines="2"
@app.get('/')
async def read_results():
results = await some_library()
return results
```
-**Note**: You can only use `await` inside of functions created with `async def`.
+!!! note
+ You can only use `await` inside of functions created with `async def`.
---
If you are using a third party library that communicates with something (a database, an API, the file system, etc) and doesn't have support for using `await`, (this is currently the case for most database libraries), then declare your endpoint functions as normally, with just `def`, like:
-```Python
+```Python hl_lines="2"
@app.get('/')
def results():
results = some_library()
For `await` to work, it has to be inside a function that supports this asynchronicity. To do that, you just declare it with `async def`:
-```Python
+```Python hl_lines="1"
async def get_burgers(number: int):
# Do some asynchronous stuff to create the burgers
return burgers
...instead of `def`:
-```Python
+```Python hl_lines="2"
# This is not asynchronous
def get_sequential_burgers(number: int):
# Do some sequential stuff to create the burgers
So, if you are using a library that tells you that you can call it with `await`, you need to create the endpoint that uses it with `async def`, like in:
-```Python
+```Python hl_lines="2 3"
@app.get('/burgers')
async def read_burgers():
burgers = await get_burgers(2)
That should make more sense now.
-All that is what powers FastAPI (through Starlette) and what makes it so powerful and have an impressive performance.
+All that is what powers FastAPI (through Starlette) and what makes it have such an impressive performance.
The key features are:
* **Fast**: Very high performance, on par with **NodeJS** and **Go** (thanks to Starlette and Pydantic).
+
+* **Fast to code**: Increase the speed to develop features by about 200% to 300% *.
+* **Less bugs**: Reduce about 40% of human (developer) induced errors. *
* **Intuitive**: Great editor support. <abbr title="also known as auto-complete, autocompletion, IntelliSense">Completion</abbr> everywhere. Less time debugging.
* **Easy**: Designed to be easy to use and learn. Less time reading docs.
-* **Short**: Minimize code duplication. Multiple features from each parameter declaration.
+* **Short**: Minimize code duplication. Multiple features from each parameter declaration. Less bugs.
* **Robust**: Get production-ready code. With automatic interactive documentation.
* **Standards-based**: Based on (and fully compatible with) the open standards for APIs: <a href="https://github.com/OAI/OpenAPI-Specification" target="_blank">OpenAPI</a> and <a href="http://json-schema.org/" target="_blank">JSON Schema</a>.
+<small>* estimation based on tests on an internal development team, building production applications.</small>
+
## Requirements
app = FastAPI()
+@app.get('/')
+def read_root():
+ return {'hello': 'world'}
+```
+
+Or if your code uses `async` / `await`, use `async def`:
+
+```Python hl_lines="6"
+from fastapi import FastAPI
+
+app = FastAPI()
+
@app.get('/')
async def read_root():
return {'hello': 'world'}
```
+!!! note
+ If you don't know, check the section about [`async` and `await` in the docs](async.md).
+
+
* Run the server with:
```bash
* an optional query parameter `q`.
-```Python
+```Python hl_lines="2 7 8 9 10 19"
from fastapi import FastAPI
from pydantic import BaseModel
* <a href="https://graphene-python.org/" target="_blank"><code>graphene</code></a> - Required for `GraphQLApp` support.
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
+Used by FastAPI / Starlette:
+
+* <a href="http://www.uvicorn.org" target="_blank"><code>uvicorn</code></a> - for the server that loads and serves your application.
You can install all of these with `pip3 install fastapi[full]`.