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(thepython3.xpart 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:
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.
- Installing
fastapi[standard]also installs libraries commonly used to run and develop servers. - Libraries installed with
pipin a virtual environment are stored in that environment'ssite-packagesfolder. - Application source files should be placed inside the virtual environment folder.
- The development server started with
fastapi devautomatically reloads saved code changes. - A FastAPI application's entry point must be named
main.py.
Answers and Explanations
- True —
standardnames a set of optional dependencies installed together. - True — The environment's
site-packagescontains the installed library code. - False — Keep application source files in the project folder, separate from the virtual environment.
- True — Development mode watches files and restarts the server when code changes.
- False — It is a convention. You can specify another filename when starting the server.