Skip to content

Getting Started with FastAPI

Preparing Your Project

Create a project folder wherever you like and navigate to it in your terminal. We will call it fastapi-basics. Create and activate a Python virtual environment inside it. Keep this environment active for the exercises that follow.

If you need help creating or navigating folders, see Basic Terminal Commands. For creating and activating an environment, see Python Virtual Environments.

Installing FastAPI

With your virtual environment active, install FastAPI:

$ pip install "fastapi[standard]"

The standard in fastapi[standard] names an optional set of extra dependencies. It installs FastAPI along with libraries commonly used to run and develop servers.

After installation, run the following command in fastapi-basics to open VS Code:

$ code .

For editor setup, see VS Code.

Look inside .venv in VS Code to find the downloaded FastAPI code. Libraries installed with pip are stored in a site-packages folder.

  • macOS: .venv/lib/python3.x/site-packages (the python3.x part depends on your Python version)
  • Windows: .venv/Lib/site-packages

This folder contains fastapi and the other installed libraries.

Installation also makes the fastapi command available in your virtual environment. Try running fastapi --version:

$ fastapi --version
FastAPI CLI version: x.y.z

If a FastAPI CLI version appears, the command is ready to use. The installed version replaces x.y.z in the example.

Running the Development Server

Let's start a web server with FastAPI.

In the VS Code Explorer, create main.py directly inside fastapi-basics, alongside .venv.

fastapi-basics/
├── .venv/
└── main.py

Add the following code to main.py and save it:

main.py
from fastapi import FastAPI  # ①

app = FastAPI()  # ②

① imports the FastAPI class from the installed library. ② creates an application object and assigns it to app. We will add request handling features to this object.

In a terminal with the environment active, navigate to the fastapi-basics folder containing main.py and run fastapi dev:

$ fastapi dev

fastapi dev finds the app object in main.py and starts the development server. When the terminal displays http://127.0.0.1:8000 and Application startup complete., the server is running.

The development server watches for code changes. Save an edited file and it restarts automatically, so you can see your changes without stopping and starting it yourself.

Open http://127.0.0.1:8000/docs in a browser. Since we have not added an API yet, the documentation has no registered operations. To stop the server, press Ctrl + C in its terminal.

The Role of main.py

main.py is a conventional filename for an application's entry point. Here it creates the FastAPI app, and fastapi dev finds its app object to start the server.

The filename is optional. If you choose another name, specify it in the command, for example fastapi dev server.py.

Check Your Understanding

Decide whether each statement is true or false.

  1. Installing fastapi[standard] also installs libraries commonly used to run and develop servers.
  2. Libraries installed with pip in a virtual environment are stored in that environment's site-packages folder.
  3. Application source files should be placed inside the virtual environment folder.
  4. The development server started with fastapi dev automatically reloads saved code changes.
  5. A FastAPI application's entry point must be named main.py.
Answers and Explanations
  1. True — standard names a set of optional dependencies installed together.
  2. True — The environment's site-packages contains the installed library code.
  3. False — Keep application source files in the project folder, separate from the virtual environment.
  4. True — Development mode watches files and restarts the server when code changes.
  5. False — It is a convention. You can specify another filename when starting the server.