FastAPI Basics

1. Install FastAPI and Uvicorn

Create a virtual environment and install fast api and Uvicorn

pip install fastapi uvicorn

2. Create main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return {"message": "Hello FastAPI"}

3. Start the FastAPI server

If your file is main.py and your FastAPI object is named app:

uvicorn main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000

4. Open the API

Open:

http://127.0.0.1:8000

You should get:

{
    "message": "Hello FastAPI"
}