Skillversitas

Hello World in FastAPI

The classic first program: an app that returns a greeting. It is also the shortest possible demonstration of the full FastAPI loop - define, run, document, test.

Install the Pieces

pip install fastapi "uvicorn[standard]"

The App

from fastapi import FastAPI

app = FastAPI(title="Hello API")

@app.get("/")
def root():
    return {"hello": "world"}

@app.get("/hello/{name}")
def hello(name: str):
    return {"message": f"Hello, {name}!"}

Save this as main.py. The app object is the ASGI application; each decorator registers a route on it.

Run It

uvicorn main:app --reload

--reload restarts the server on file changes during development. Try it:

curl http://127.0.0.1:8000/
# {"hello":"world"}
curl http://127.0.0.1:8000/hello/Skillversitas
# {"message":"Hello, Skillversitas!"}

Check the Docs

Open http://127.0.0.1:8000/docs - Swagger UI lists both endpoints with request forms generated from your type hints. The /redoc endpoint renders the same OpenAPI schema in a reference style.

What Just Happened

  1. Uvicorn imported main and found the app attribute.
  2. FastAPI registered two routes from the decorators.
  3. Each request was matched to a route, the path parameter was extracted and validated, and the dict return value was serialized to JSON.

Summary

A FastAPI hello world is a file with an app = FastAPI() object and one or more decorated route functions, run with uvicorn main:app --reload. The framework serializes your return values to JSON and generates interactive docs at /docs - the smallest API and the whole workflow in about ten lines.