fastAPI

fastAPI

Language
Python
Category
9
Version
0.70.0
Authors
Tiangolo
Description
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
Install Instructions
$ pip install fastapi
  • You'll also need an ASGI server for production such as Uvicorn or Hypercorn.
$ pip install "uvicorn[standard]"
Usage
  • Create a file "main.py"
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}
  • Run the server with:
$ uvicorn main:app --reload
***** OUTPUT *****
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.
  • Now check it out by opening your browser at "http://127.0.0.1:8000/items/5?q=somequery"
  • The JSON response should be:
{"item_id": 5, "q": "somequery"}
Links