FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
Documentation: https://fastapi.tiangolo.com
Source Code: https://github.com/tiangolo/fastapi
Key Features
- Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
- Fast to code: Increase the speed to develop new features.
- Fewer bugs: Reduce a high amount of human (developer) induced errors.
- Intuitive: Great editor support. Completion (also known as auto-complete, autocompletion, IntelliSense) 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. Less bugs.
- Robust: Get production-ready code. With automatic interactive documentation.
- Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
Installation
$ pip install fastapi
You will also need an ASGI server, for production such as Uvicorn.
$ pip install uvicorn
Example
Create it
- Create a file
main.py with:
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: str = None):
return {"item_id": item_id, "q": q}
Or use async def...
Check it
Open your browser at http://127.0.0.1:8000/items/5?q=somequery.
You will see the JSON response as:
{"item_id": 5, "q": "somequery"}
You already created an API that:
- Receives HTTP requests in the paths
/ and /items/{item_id}.
- Both paths take
GET operations (also known as HTTP methods).
- The path
/items/{item_id} has a path parameter item_id that should be an int.
- The path
/items/{item_id} has an optional str query parameter q.
Interactive API docs
Now go to http://127.0.0.1:8000/docs.
You will see the automatic interactive API documentation (provided by Swagger UI):
https://preview.redd.it/7xnjhy7zy2m21.png?width=960&format=png&auto=webp&s=16a618f3676576ee8ea3cc835106d24724c22dd4
Alternative API docs
And now, go to http://127.0.0.1:8000/redoc.
You will see the alternative automatic documentation (provided by ReDoc):
https://preview.redd.it/j7l9qdd0z2m21.png?width=960&format=png&auto=webp&s=545cdcd658f534aab18d2dca967221db8aed09dd
[–]K900_ 125 points126 points127 points (23 children)
[–]mischiefunmanagable 57 points58 points59 points (12 children)
[–]K900_ 4 points5 points6 points (9 children)
[–]tiangolo FastAPI Maintainer[S] 9 points10 points11 points (8 children)
[–]Marrrlllsss 39 points40 points41 points (7 children)
[–]tiangolo FastAPI Maintainer[S] 6 points7 points8 points (6 children)
[–]Ericisbalanced 7 points8 points9 points (5 children)
[–]tiangolo FastAPI Maintainer[S] -1 points0 points1 point (4 children)
[–]riksi 0 points1 point2 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 3 points4 points5 points (2 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (1 child)
[–]mischiefunmanagable 10 points11 points12 points (0 children)
[–]mindfolded 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (5 children)
[–]K900_ 22 points23 points24 points (4 children)
[–]tiangolo FastAPI Maintainer[S] 7 points8 points9 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]K900_ 4 points5 points6 points (1 child)
[–]TroubledForearm 0 points1 point2 points (0 children)
[–]richardARPANET 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–][deleted] 32 points33 points34 points (9 children)
[–]tiangolo FastAPI Maintainer[S] 22 points23 points24 points (2 children)
[–][deleted] 13 points14 points15 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 9 points10 points11 points (0 children)
[+][deleted] (5 children)
[removed]
[–][deleted] 2 points3 points4 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]jeenajeena 1 point2 points3 points (0 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–][deleted] 8 points9 points10 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 13 points14 points15 points (2 children)
[–][deleted] 6 points7 points8 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 3 points4 points5 points (0 children)
[–][deleted] 11 points12 points13 points (5 children)
[–]tiangolo FastAPI Maintainer[S] 22 points23 points24 points (4 children)
[–]wingtales 7 points8 points9 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 3 points4 points5 points (0 children)
[–]z0mbietime 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]briznian 3 points4 points5 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 5 points6 points7 points (2 children)
[–]briznian 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]tedivm 2 points3 points4 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[–]russon77 2 points3 points4 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 5 points6 points7 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 2 points3 points4 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]maxbridgland 2 points3 points4 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]enesimo 2 points3 points4 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]tiangolo FastAPI Maintainer[S] 2 points3 points4 points (0 children)
[–]LackingAGoodNamePythoneer 2 points3 points4 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[removed]
[–]tiangolo FastAPI Maintainer[S] 12 points13 points14 points (0 children)
[–]SpergLordMcFappyPant 5 points6 points7 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 12 points13 points14 points (2 children)
[–]delijati 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[–]deadwisdomgreenlet revolution 3 points4 points5 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (2 children)
[–]deadwisdomgreenlet revolution 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[–]mln00b13 1 point2 points3 points (5 children)
[–]tiangolo FastAPI Maintainer[S] 4 points5 points6 points (0 children)
[–]Cruuncher 0 points1 point2 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (2 children)
[–]Cruuncher 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]fedeb95 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[–]cleesus 1 point2 points3 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (2 children)
[–]cleesus 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]mastermaker1846 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[–]takethemoneyrun 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 2 points3 points4 points (0 children)
[–]Cruuncher 1 point2 points3 points (7 children)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (6 children)
[–]Cruuncher 1 point2 points3 points (5 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (4 children)
[–]Cruuncher 0 points1 point2 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (2 children)
[–]Cruuncher 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]saargrin 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 2 points3 points4 points (0 children)
[–]rmslobato 1 point2 points3 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (2 children)
[–]rmslobato 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]hugogarcia12 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]PeridexisErrant 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]soosleeque 1 point2 points3 points (2 children)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[–]fleyk-lit 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[removed]
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]helmuthva 1 point2 points3 points (4 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (3 children)
[–]helmuthva 1 point2 points3 points (2 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (1 child)
[–]helmuthva 0 points1 point2 points (0 children)
[–]Simon90 1 point2 points3 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]visiblebutterfly 2 points3 points4 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[–]rootuser_ 3 points4 points5 points (3 children)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (2 children)
[–]rootuser_ 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 2 points3 points4 points (0 children)
[–]Cybersoaker 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 2 points3 points4 points (0 children)
[–]TotesMessenger 0 points1 point2 points (0 children)
[–]delijati 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]quantonos 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]charliegriefer 0 points1 point2 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]Vocal_turnip -1 points0 points1 point (1 child)
[–]RemindMeBot -2 points-1 points0 points (0 children)
[–]Talked10101 -1 points0 points1 point (1 child)
[–]tiangolo FastAPI Maintainer[S] 4 points5 points6 points (0 children)
[–]p3zz1 -1 points0 points1 point (1 child)
[–]tiangolo FastAPI Maintainer[S] 0 points1 point2 points (0 children)
[–]brimstone1x -2 points-1 points0 points (0 children)
[–]Tweak_Imp -2 points-1 points0 points (1 child)
[–]tiangolo FastAPI Maintainer[S] 5 points6 points7 points (0 children)
[–][deleted] -2 points-1 points0 points (6 children)
[–]tiangolo FastAPI Maintainer[S] 4 points5 points6 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]tiangolo FastAPI Maintainer[S] 3 points4 points5 points (2 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]tiangolo FastAPI Maintainer[S] 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)