- Courses
- 📚Libraries & Frameworks
- ⚡FastAPI
- Foundations of FastAPI
- Introduction
On this page
Getting Started with FastAPI
Published on August 9, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
FastAPI is a Python web framework for building APIs, built on top of Starlette for the web layer and Pydantic for data validation, and designed around one core idea: the type hints already written in a function's parameters double as validation rules and as the source for automatically generated documentation. A parameter annotated as int is checked as an integer before the function body ever runs, and that same annotation shows up in an interactive documentation page without any separate schema file being written by hand.
The sections below install FastAPI and its server, build a minimal working API, and show — with a real example rather than just a description — how that type-hint-driven validation behaves when the input doesn't match what was expected.
What FastAPI Is and Why It's Used​
Why FastAPI is considered fast​
FastAPI's own documentation describes its performance as on par with Node.js and comparable to Go for many API workloads, and this comes down to what it's built on rather than anything unique to FastAPI itself: Starlette is an ASGI framework, meaning it handles requests asynchronously rather than blocking a thread per request, and Pydantic's validation layer is written with performance as an explicit design goal. Among Python web frameworks specifically, this combination puts FastAPI toward the fast end of the spectrum. For genuinely CPU-bound work — heavy computation rather than waiting on network or database calls — a compiled language like Go still tends to outperform any Python framework, FastAPI included, so "fast" here is best understood as fast among Python options and competitive with Node.js for typical I/O-bound API traffic, not a universal claim across every language and workload.
Automatic validation and documentation from type hints​
A FastAPI path operation function declares its expected inputs using ordinary Python type hints:
def read_item(item_id: int):
...
Because item_id is annotated as int, FastAPI validates incoming requests against that type automatically, before the function body runs — a request where item_id isn't a valid integer gets rejected with a structured error response, with no manual validation code written anywhere. The same annotations are read to generate an OpenAPI schema automatically, which powers two interactive documentation interfaces reachable directly from a running app, covered further down. Nothing about the documentation needs to be kept in sync manually, since it's generated from the same type hints the code already relies on for validation.
Where FastAPI fits​
FastAPI is most commonly reached for when building RESTful APIs and microservices, since its validation and async support scale well to many small, independent services. It also shows up frequently as the backend for web and mobile applications, as a serving layer for machine learning models where request validation matters as much as the model output itself, and — through its native WebSocket support — in real-time applications that need a persistent connection rather than repeated request/response calls.
Installing FastAPI and Uvicorn​
FastAPI handles the API logic, but it doesn't run a server on its own — that's the job of an ASGI server, and Uvicorn is the one most commonly paired with it.
Creating an isolated environment​
Installing into a virtual environment keeps this project's dependencies separate from anything else installed on the system:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Installing FastAPI with the standard extras​
pip install "fastapi[standard]"
The [standard] extra pulls in Uvicorn along with a handful of other commonly needed packages in a single install, rather than installing FastAPI and Uvicorn as two separate commands. The quotes around fastapi[standard] matter in some shells — without them, certain terminals interpret the square brackets as a wildcard pattern instead of passing the literal string to pip.
Verifying the installation​
python -m pip show fastapi
python -m pip show uvicorn
Both commands print the installed version and location if the install succeeded, confirming the environment is ready for the next step.
Building a Minimal Hello World API​
Create a file named main.py:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}
FastAPI() creates the application instance that Uvicorn will run. @app.get("/") registers the function below it as the handler for GET requests to the root path / — this is what FastAPI calls a path operation. The function returns a plain Python dictionary, and FastAPI serializes it to a JSON response automatically; nothing needs to be converted to JSON manually.
Running the development server​
The modern, recommended way to run this locally uses the fastapi command line tool, included automatically with the [standard] extra installed earlier:
fastapi dev main.py
This starts a development server with auto-reload already enabled, watching main.py for changes and restarting automatically. The same result can also be reached by invoking Uvicorn directly:
uvicorn main:app --reload
main refers to the filename without its .py extension, app is the FastAPI instance created inside it, and --reload enables the same auto-restart-on-change behavior — useful to know since a lot of existing FastAPI code and tutorials still use this form.
What each endpoint returns​
With the server running, three URLs are available by default:
http://127.0.0.1:8000/returns the JSON response{"message": "Hello, FastAPI!"}directly from the function above.http://127.0.0.1:8000/docsopens Swagger UI, an interactive documentation page generated from the app's routes and type hints, where each endpoint can be expanded and called directly from the browser.http://127.0.0.1:8000/redocopens ReDoc, an alternative documentation layout built from the same underlying schema, generally better suited to reading through an API's full surface rather than testing individual calls.
Adding a Parameterized Endpoint​
A second route accepting a path parameter shows the same type-hint mechanism at work on dynamic input:
@app.get("/greet/{name}")
def greet_user(name: str):
return {"message": f"Hello, {name}!"}
Visiting http://127.0.0.1:8000/greet/Alice returns {"message": "Hello, Alice!"}, with name captured directly from the URL segment and passed into the function as a str.
The validation behavior described earlier becomes visible by changing the annotation to something narrower. Replacing name: str with an int-typed parameter —
@app.get("/multiply/{number}")
def multiply_by_two(number: int):
return {"result": number * 2}
— and visiting http://127.0.0.1:8000/multiply/5 returns {"result": 10} as expected. Visiting http://127.0.0.1:8000/multiply/five instead, with a value that can't be parsed as an integer, returns a 422 Unprocessable Entity response describing exactly which field failed validation and why — without a single line of validation code written for that route. This is the concrete effect of the type hint declared on number: it isn't just documentation for a human reader, it's an active check enforced on every request.
Key Takeaways​
FastAPI's core mechanism is straightforward once seen in action: a type hint on a function parameter is simultaneously a validation rule, enforced automatically on incoming requests, and the source of the interactive documentation available at /docs and /redoc. Installing with pip install "fastapi[standard]" brings in Uvicorn and related tooling in one step, and fastapi dev main.py starts a local server with reload enabled without needing Uvicorn invoked directly, though the manual uvicorn main:app --reload form remains equally valid and common. From a two-route Hello World app, the same pattern — an ASGI server, path operations decorated with HTTP methods, and type-hinted parameters — scales up to the REST APIs, microservices, and backend services FastAPI is most commonly used to build.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Foundations of FastAPI
Next
Basic Routing
-
What FastAPI Is and Why It's Used
- Why FastAPI is considered fast
- Automatic validation and documentation from type hints
- Where FastAPI fits
-
Installing FastAPI and Uvicorn
- Creating an isolated environment
- Installing FastAPI with the standard extras
- Verifying the installation
-
Building a Minimal Hello World API
- Running the development server
- What each endpoint returns
-
Adding a Parameterized Endpoint
-
Key Takeaways
