Learn / Frameworks / FastAPI / Introduction and First Endpoint

FastAPI · Lesson 1 of 5

Introduction and First Endpoint

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

  • Beginner
  • 11 min read
  • 3 objectives

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).

Install and run

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.

# Write your solution here
Up next · Lesson 2Path, Query and Body ParametersAccept data from the URL and request body with type validation.