FastAPI Folder FastAPI Structure

By Benjamin

When building applications with FastAPI, maintaining a clean and organized project structure is crucial for scalability and maintainability. A well-structured project not only aids collaborative development but also facilitates easier navigation and understanding of your codebase. This article explores the recommended FastAPI folder structure, helping you create a robust and SEO-friendly project right from the start.

a purple background with yellow dots and the words folder structure on top of each one
a purple background with yellow dots and the words folder structure on top of each one

Before diving into the folder structure, let's briefly touch upon the reasons behind the recommendations. A proper folder structure promotes code separation, enhances readability, and simplifies debugging. It also makes your project more appealing to developers who might contribute to or maintain your project in the future.

A systematic approach for managing project folder structures
A systematic approach for managing project folder structures

High-Level Project Organization

FastAPI projects typically follow a package structure, with the main entry point being the FastAPI application. This top-level structure ensures a clear separation of concerns and promotes overall project organization.

Flutter Folder Structure
Flutter Folder Structure

Here's a high-level overview of the recommended FastAPI folder structure:

  • project_root/
  • – app/main.py
  • – app/core/)
  • – app/models/)
  • – app/schemas/)
  • – app/services/
  • – app/utils/
  • – app/tests/)
  • – config/
  • – logs/
  • – .env
  • – Dockerfile
  • – requirements.txt
  • – README.md
  • – LICENSE
the flow diagram shows how to use it in an application with multiple functions and features
the flow diagram shows how to use it in an application with multiple functions and features

app/main.py: The FastAPI entry point

Main.py serves as the entry point for your FastAPI application. It initializes the FastAPI instance and mounts the routers for your application. This file should not contain any business logic, focusing solely on application initialization and routing configuration.

Example:

React Folder Structure for Beginners: The Folder O
React Folder Structure for Beginners: The Folder O

from fastapi import FastAPI
from app.routes import router

app = FastAPI()

app.include_router(router, prefix="/api")

app/core: Configuration and base models

The core package contains the base models for your application and configuration-related settings. It should not include any business logic or API-specific code. The base models can then be extended in the app/models package.

app/models: Database models

a diagram with different types of words and numbers
a diagram with different types of words and numbers

The models package houses all your SQLAlchemy or Tortoise ORM-related models. Follow the Single Responsibility Principle, ensuring each model represents a single entity or data source in your database.

API-specific Organization

Folder Structure - Dragonframe - Help Wiki
Folder Structure - Dragonframe - Help Wiki
A Website Design Folder Structure
A Website Design Folder Structure
What is your file naming convention you use for version control?
What is your file naming convention you use for version control?
an image of a business organization chart
an image of a business organization chart
an image of a diagram with folders on it
an image of a diagram with folders on it
The Best Way to Organize Marketing Assets With Simple Folder Structures
The Best Way to Organize Marketing Assets With Simple Folder Structures
Digital Project Management Log for Teams
Digital Project Management Log for Teams
the construction tools list is shown here
the construction tools list is shown here
Node.js Project Folder Structure
Node.js Project Folder Structure
the inside and outside of a brochure, with different designs on each side
the inside and outside of a brochure, with different designs on each side

A well-architected FastAPI application divides API-related code into separate packages – schemas, services, and routers. This promotes code separation, enhancing maintainability and testability.

app/schemas: Pydantic models for input/output validation

Schemas contain Pydantic models used for input validation and output serialization. Each API endpoint should have corresponding input and output schemas that validate and serialize the data passed to and from the API.

app/services: Business logic and API-specific functions

Services encapsulate the business logic related to each API endpoint. They should contain functions that operate on the models and schemas, performing the necessary computations and database operations to handle the API request.

app/utils: Utility functions and tools

The utils package includes any utility functions or tools that do not belong to a specific API endpoint or model. This promotes code organization and separation of concerns, making it easier to maintain and reason about your codebase.

app/tests: Unit tests and integration tests

The tests package contains unit tests for your models, services, and API endpoints. Follow the Arrange-Act-Assert (AAA) pattern and ensure the tests are isolated, independent, and atomic.

Proper folder structure offers numerous benefits, making your FastAPI project more organized, maintainable, and SEO-friendly. By following the recommended structure, you can significantly enhance the overall developer experience and attract potential contributors to your project. So, start structuring your FastAPI projects correctly today and unlock the true potential of the framework!