FastAPI framework, high performance, easy to learn, fast to code, ready for production
Documentation: https://fastapi.tiangolo.com
Source Code: https://github.com/tiangolo/fastapi
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
The key features are:
* estimation based on tests on an internal development team, building production applications.
$ pip install fastapi
---> 100%
$ pip install "uvicorn[standard]"
---> 100%
main.py
with:from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
async def
...If your code uses async
/ await
, use async def
:
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
Note:
If you don't know, check the "In a hurry?" section about async
and await
in the docs.
$ uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.
uvicorn main:app --reload
...The command uvicorn main:app
refers to:
main
: the file main.py
(the Python "module").app
: the object created inside of main.py
with the line app = FastAPI()
.--reload
: make the server restart after code changes. Only do this for development.{"item_id": 5, "q": "somequery"}
/
and /items/{item_id}
.GET
operations (also known as HTTP methods)./items/{item_id}
has a path parameter item_id
that should be an int
./items/{item_id}
has an optional str
query parameter q
.from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: Union[bool, None] = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_name": item.name, "item_id": item_id}
item_id: int
item: Item
str
, int
, float
, bool
, list
, etc).datetime
objects.UUID
objects.item_id
in the path for GET
and PUT
requests.item_id
is of type int
for GET
and PUT
requests.q
(as in http://127.0.0.1:8000/items/foo?q=somequery
) for GET
requests.q
parameter is declared with = None
, it is optional.None
it would be required (as is the body in the case with PUT
).PUT
requests to /items/{item_id}
, Read the body as JSON:name
that should be a str
.price
that has to be a float
.is_offer
, that should be a bool
, if present. return {"item_name": item.name, "item_id": item_id}
... "item_name": item.name ...
... "item_price": item.price ...
maximum_length
or regex
.requests
and pytest
ujson
- for faster JSON "parsing".email_validator
- for email validation.requests
- Required if you want to use the TestClient
.jinja2
- Required if you want to use the default template configuration.python-multipart
- Required if you want to support form "parsing", with request.form()
.itsdangerous
- Required for SessionMiddleware
support.pyyaml
- Required for Starlette's SchemaGenerator
support (you probably don't need it with FastAPI).ujson
- Required if you want to use UJSONResponse
.uvicorn
- for the server that loads and serves your application.orjson
- Required if you want to use ORJSONResponse
.
© 2018 Sebastián Ramírez
Licensed under the MIT License.
https://fastapi.tiangolo.com/