Installation & First App

Install

Example

Create a virtual environment and install FastAPI with the standard extras (includes the Uvicorn server and the fastapi CLI):

python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install "fastapi[standard]"

Example output

Successfully installed fastapi-0.115.x uvicorn-0.32.x pydantic-2.x starlette-0.41.x ...

Hello, FastAPI

Example

Save this as main.py. Every decorated function is an endpoint; returning a dict sends JSON:

from fastapi import FastAPI

app = FastAPI()

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

@app.get("/health")
def health():
    return {"status": "ok"}

Requests and responses

GET /
→ 200 {"message": "Hello, FastAPI!"}
GET /health
→ 200 {"status": "ok"}

Run the Server

Example

fastapi dev starts Uvicorn with auto-reload; open http://127.0.0.1:8000:

fastapi dev main.py

Example output

INFO     Using path main.py
INFO     Serving at: http://127.0.0.1:8000
INFO     API docs: http://127.0.0.1:8000/docs
INFO     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Note: In production use fastapi run main.py (no reload) or uvicorn main:app --host 0.0.0.0 --port 8000.