FastAPI Basics: Build Your First Web API
What Is FastAPI?
In this lesson you will
- Explain what a web API is and what an endpoint does
- Install FastAPI and run a first app with the development server
- Open the automatic interactive documentation
Introduction
A web API is a program that other programs talk to over the web. A phone app asks an API for a list of books, and the API replies with data, usually as JSON. FastAPI is a modern Python framework for building such APIs. It uses Python type hints to validate input, convert data and generate documentation for you automatically.
You should know basic Python (functions, dictionaries, type hints) before this course. The code on this page was run with Python 3.13 and FastAPI 0.162. Versions change, so check the official documentation if something behaves differently.
How an API request works
- A client sends an HTTP request: a method (GET, POST, PUT, DELETE), a path such as
/books/42, optional data. - Your code runs and decides what to do.
- The server sends back an HTTP response: a status code (200 OK, 404 Not Found) and usually a JSON body.
Each path and method pair you handle is called an endpoint or route.
Install and set up
Use a virtual environment so the project's packages stay separate.
Terminal
python -m venv venv
venv\Scripts\activate
pip install fastapi uvicornOn macOS and Linux, activate with source venv/bin/activate. Uvicorn is the server that runs your app. Package names and recommended extras can change, so follow the current installation page of the FastAPI documentation if the command above fails.
Your first endpoint
Create main.py:
Python
from fastapi import FastAPI
app = FastAPI(title="Library API")
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI"}
@app.get("/books/{book_id}")
def get_book(book_id: int):
return {"book_id": book_id}Reading it:
app = FastAPI()creates the application.@app.get("/")is a decorator that registers the function below it for GET requests to/.- The function returns a normal Python dictionary. FastAPI converts it to JSON.
{book_id}in the path is a path parameter, andbook_id: inttells FastAPI to convert it to an integer.
Run it
Terminal
uvicorn main:app --reloadmain:app means "the object called app in the file main.py". --reload restarts the server when you edit the code, which is useful in development only.
Open http://127.0.0.1:8000/ in a browser. You should see:
JSON
{"message": "Hello, FastAPI"}And http://127.0.0.1:8000/books/42 returns {"book_id": 42}. These are the responses the code above produced when I ran it with FastAPI's test client.
Quick quiz
What does the decorator @app.get('/') do?
Automatic documentation
FastAPI builds an OpenAPI description of your API from your code, and serves two interactive pages:
http://127.0.0.1:8000/docs: Swagger UI. You can try endpoints directly in the browser.http://127.0.0.1:8000/redoc: an alternative reading view.
The raw description is at /openapi.json. This documentation always matches your code, because it is generated from it.
Why people choose FastAPI
- Type hints do the work: validation, conversion and documentation.
- Fast to write, with editor autocompletion.
- Good performance among Python frameworks, and support for async code.
- Standards-based: OpenAPI and JSON Schema.
It is not the only choice. Django, Flask and others are also widely used. Choose based on your project.
Safety note
--reload and the docs pages are for development. Later lessons cover what to change before putting an API on the internet.
Try it yourself
Create the project, run the server and open /docs. Add a new endpoint /about that returns your name and the API version, refresh the docs and call it from the page.
Summary
- A web API receives HTTP requests and returns data, usually as JSON, and each path and method pair is an endpoint.
- FastAPI uses decorators and type hints to define routes, convert data and generate OpenAPI documentation.
- Run with
uvicorn main:app --reloadin development and explore/docs.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading