HomeToolsAbout

FastAPI Basics

Initialize

No initialization needed

  • Create a main.py python script with basic route
# main.py from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"}

Spinning Up Server

Using uvicorn

uvicorn is a ASGI server (Asynchronous Server Gateway Interface)

  • used to run asynchronous web applications
  • ASGI is a specification for asynchronous web servers and applications in Python
pip install uvicorn uvicorn <app_name>:app --reload

Swagger Doc

FastAPI comes with Swagger UI for documentation

http://localhost:8000/docs

Basic syntax

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, query_param: str = None): return {"item_id": item_id, "query_param": query_param}
AboutContact