Guide · Open source
How to use FastAPI: install, write endpoints and read the docs
Updated · written with AI from the project's own documentation and checked against our catalogue data
FastAPI is an open-source Python framework for building web APIs, the back-end services that apps and websites use to send and receive data. It is MIT licensed and built on Starlette and Pydantic. This guide shows you how to install it, write a small app from the example in the project README, and use the interactive documentation it generates.
Quick start
- 1Install uv. Follow the uv installation guide linked in the README, then open a terminal in your project folder.
- 2Add FastAPI. Run uv add "fastapi[standard]" to add FastAPI to your project.
- 3Create main.py. Create a file named main.py containing the app and the two endpoints from the example.
- 4Define your endpoints. Use @app.get decorators and Python type hints for each parameter so FastAPI can validate input.
- 5Read the docs. Open the interactive documentation generated from your code and try your endpoints there.
What FastAPI is and who it suits
You define endpoints with standard Python type hints, and FastAPI uses those hints to validate input and generate interactive API documentation. The project describes itself as based on standard Python type hints and fully compatible with the open standards OpenAPI and JSON Schema.
It suits Python developers building JSON APIs or back-end services, teams that want automatic interactive docs, and ML teams serving model predictions over HTTP. It assumes familiarity with type hints and async concepts, so it is a poor first Python project.
- Good fit: JSON APIs and back-end services written in Python
- Good fit: teams that want docs generated from code
- Not a fit: full-stack sites needing built-in templates or an admin panel
- Not a fit: non-Python teams, since the framework is written for Python
Install FastAPI with uv or pip
The README recommends installing uv first, then adding FastAPI to your project. Put the package name in quotes so it works in all terminals.
If you prefer pip, install fastapi[standard] inside a virtual environment. The README points to its installation guide for the alternative steps.
- Prerequisite: Python (the README excerpt does not state a minimum version; check the supported versions badge on PyPI)
- Recommended tool: uv, or pip inside a virtual environment
uv add "fastapi[standard]"Create your first app in main.py
Create a file named main.py. It creates a FastAPI app and defines two GET endpoints. The root path returns a small JSON object, and the second path takes an item_id parameter declared as an int, plus an optional query parameter q.
Because the parameter types are declared, FastAPI validates the incoming values for you.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}Use async def when your code needs it
The README example uses plain def functions. It also includes an expandable section on using async def, which applies when your code uses async and await. Check the official documentation for the details of when to choose each form.
Explore the automatic interactive docs
FastAPI generates interactive API documentation from your code. The README says this documentation is included in the framework and is reachable from the project's documentation site. Use the docs to see your endpoints and try them out.
The project also links a separate FastAPI mini documentary and the Typer library for command-line apps, which are useful if you want to go further.
- Interactive docs are generated automatically from your type hints
- Documentation is hosted at fastapi.tiangolo.com
What people use it for
- Serving JSON endpoints for a web or mobile app back end
- Serving machine learning model predictions over HTTP, as several cited examples do
- Building internal back-end services that other teams call through an API
- Creating API-first services where generated docs help other developers
Frequently asked questions
Is FastAPI free to use?
Yes. FastAPI is released under the MIT licence, which the project lists alongside its source code. You can use it in commercial and personal projects under that licence's terms.
What do I need to run FastAPI?
You need Python and either uv or pip to install the package. The README excerpt does not state a minimum Python version, so check the supported versions shown on the PyPI page.
How do I install FastAPI?
Install uv, then run uv add "fastapi[standard]" in your project folder. If you use pip, install fastapi[standard] inside a virtual environment.
What is FastAPI built on?
FastAPI uses Starlette for the web layer and Pydantic for data handling. The README lists both as the foundations the framework stands on.
How does FastAPI compare to Flask or Django REST Framework?
The README names Flask, Django REST Framework, Litestar and Starlette as comparable projects but does not benchmark them against FastAPI. FastAPI's own pitch is type-hint validation and automatic docs for APIs; Django is better suited if you need a full-stack framework with an admin panel.
Why do I get an error when I install fastapi[standard]?
The README notes that the package name must be in quotes so it works in all terminals. If you still see errors, check that your Python version is supported and consult the official installation guide.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
FastAPI suits Python developers who want to build JSON APIs with less boilerplate and automatic interactive docs. If you need a full-stack framework with templates and an admin panel, or you are new to Python and to type hints, pick something like Django or Flask instead.
Install options & downloads