-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

FastAPI Cookbook
By :

Setting up a well-organized project structure is crucial for maintaining a clean code base, especially as your application grows and evolves. This recipe will guide you on how to create your first basic FastAPI project. A structured project simplifies navigation, debugging, and collaboration. For FastAPI, following best practices in structuring can significantly enhance scalability and maintainability.
All you need to do to follow the recipe is make sure that you have your development environment set up.
We begin by making a project folder named fastapi_start
that we’ll use as the root project folder.
$ python -m venv .venv
This will create a .venv
folder that will contain all packages required for the project within our project's root folder.
$ source .venv/bin/activate
From Windows, run the following command:
$ .venv\Scripts\activate
When the environment is active, you should see in your terminal a prefix string such as (.venv) $
. Alternatively, if you check the location of the python
binary command, it should be located within the .venv
folder. From now on, each time you install a module with pip
, it will be installed in the .venv
folder, and it will be activated only if the environment is active.
fastapi
package with uvicorn
in your environment by running the following command:$ pip install fastapi uvicorn
Once FastAPI is installed in your environment, open your project folder with your favorite IDE and create a file called main.py
.
FastAPI
module. Then, create an instance of the FastAPI
class:from fastapi import FastAPI app = FastAPI()
This instance houses the code of your application.
@app.get("/") def read_root(): return {"Hello": "World"}
You’ve just created the code for your first FastAPI application.
If you want to track the project, you can set up Git as follows:
$ git init
This simple command prepares your project for version control under Git.
Before committing, create a .gitignore
file to specify untracked files to ignore (such as __pychache__
, .venv
, or IDE-specific folders). You can also have a look at the one on the GitHub repository of the project at the link: https://github.com/PacktPublishing/FastAPI-Cookbook/blob/main/.gitignore.
$ git add .
$ git commit -m "Initial commit"
And that's it. You are now tracking your project with Git.
A well-structured project is not just about neatness; it’s about creating a sustainable and scalable environment where your application can grow and evolve. In FastAPI, this means organizing your project in a way that separates different aspects of your application logically and efficiently.
There is no unique and perfect structure for a FastAPI project; however, a common approach is to divide your project into several key directories:
/src
: This is where your primary application code lives. Inside /src
, you might have subdirectories for different modules of your application. For instance, you could have a models
directory for your database models, a routes
directory for your FastAPI routes, and a services
directory for business logic./tests
: Keeping your tests separate from your application code is a good practice. It makes it easier to manage them and ensures that your production builds don’t include test code./docs
: Documentation is crucial for any project. Whether it’s API documentation, installation guides, or usage instructions, having a dedicated directory for documentation helps maintain clarity.You can find detailed information on how to manage virtual environments with venv
at the following link:
To brush up your knowledge with Git and get familiar with adding, staging and commiting operations, have a look at this guide: