Learn / Frameworks / FastAPI / Introduction and First Endpoint

Beginner 11 min

Introduction and First Endpoint

Install FastAPI, write an endpoint and explore the automatic docs.

What you will learn

  • Run a FastAPI app
  • Define path operations
  • Open /docs

FastAPI is a modern Python framework for building APIs. It is fast, uses standard Python type hints for validation, and generates interactive documentation automatically. It is built on Starlette (web layer) and Pydantic (data validation).

python3 -m venv .venv && source .venv/bin/activate
pip install "fastapi[standard]"
# main.py
from fastapi import FastAPI

app = FastAPI(title="Demo API")

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

@app.get("/health")
def health():
    return {"status": "ok"}
fastapi dev main.py      # or: uvicorn main:app --reload

The --reload behavior restarts the server when you save a file. Visit http://127.0.0.1:8000/ to see the JSON.

Automatic documentation

Two free UIs appear the moment you have endpoints:

  • /docs: Swagger UI, where you can try each endpoint from the browser.
  • /redoc: a cleaner reference view.
  • /openapi.json: the machine-readable OpenAPI schema; front-end tools can generate clients from it.

Path operations

A path operation is a function plus a decorator naming the HTTP method and URL path: @app.get, @app.post, @app.put, @app.delete. Whatever the function returns (dicts, lists, Pydantic models) is converted to JSON.

Why type hints matter here

In the next lessons you will see that the annotations you write, such as item_id: int, are not decoration: FastAPI reads them to convert, validate and document input automatically.

FastAPI vs Flask vs Django

Flask is minimal and unopinionated. Django is full-stack with an ORM and admin. FastAPI focuses on APIs, with validation, docs and async support built in.

Try it yourself

Add a GET /about endpoint that returns your name and app version, then open /docs and call it from the browser.

Show solution
@app.get("/about")
def about():
    return {"author": "Amar", "version": "1.0.0"}