top of page

Folder Structure for Scalable FastAPI Applications in Production

Creating a well-organized folder structure for your FastAPI app can contribute to code readability, maintainability, and scalability.


Comparing Basic Project Folder Structure to a Scalable Application Structure
Comparing Basic Project Folder Structure to a Scalable Application Structure

Set up a basic FastAPI project structure.

Setting up a basic FastAPI project structure involves organizing your code in a way that is modular, scalable, and easy to maintain. Below is a simple project structure for a FastAPI application. Note that this is just one possible structure, and you can adapt it based on your specific needs.



my_fastapi_app/
|-- app/
|   |-- __init__.py
|   |-- main.py         # Main FastAPI application
|   |-- api/
|       |-- __init__.py
|       |-- endpoints/  # FastAPI route handlers
|           |-- __init__.py
|           |-- example.py
|-- requirements.txt    # Dependencies file
|-- main.py             # Entry point for the application




Explanation of the structure:

  • my_fastapi_app/: This is the root directory of your project.

  • app/: This directory contains your FastAPI application code.

  • init.py: This file makes the app directory a Python package.

  • main.py: This file contains the main FastAPI application. You can define your FastAPI app instance, configure it, and include additional settings.

  • api/: This directory organizes your API-related code.

  • init.py: This file makes the api directory a Python package.

  • endpoints/: This directory contains your FastAPI route handlers.

  • init.py: This file makes the endpoints directory a Python package.

  • example.py: An example FastAPI route handler. You can have multiple files in this directory for different parts of your API.

  • requirements.txt: This file lists the dependencies of your project. You can use it with tools like pip to install the required packages.

  • main.py: This file serves as the entry point for your application. You might use it to run the FastAPI development server or perform other initialization tasks.



This is a minimalistic structure, and as your project grows, you might want to consider adding directories for database models, schemas, utilities, tests, and static files. You can adapt this structure based on your specific application requirements and development preferences.


Keep in mind that this is a starting point, and as your project evolves, you may need to refactor or extend the structure to accommodate additional features and functionalities.



 

Optimal Folder Structure for Scalable FastAPI Applications in Production


Below is a suggested folder structure for a production-level FastAPI application designed for scalability and maintainability:



my_fastapi_app/
│
├── app/
│   ├── __init__.py
│   ├── main.py
│   ├── api/
│   │   ├── __init__.py
│   │   ├── v1/
│   │   │   ├── __init__.py
│   │   │   ├── endpoints/
│   │   │   │   ├── __init__.py
│   │   │   │   ├── example.py
│   │   ├── v2/
│   │   │   ├── __init__.py
│   │   │   ├── endpoints/
│   │   │   │   ├── __init__.py
│   │   │   │   ├── another_example.py
│   │
│   ├── core/
│   │   ├── __init__.py
│   │   ├── config.py
│   │   ├── security.py
│   │   ├── database.py
│   │
│   ├── dependencies/
│   │   ├── __init__.py
│   │   ├── authentication.py
│   │
│   ├── models/
│   │   ├── __init__.py
│   │   ├── example.py
│   │   ├── another_example.py
│   │
│   ├── tests/
│   │   ├── __init__.py
│   │   ├── test_example.py
│   │   ├── test_another_example.py
│   │
│   ├── utils/
│   │   ├── __init__.py
│   │   ├── helpers.py
│   │
│   ├── main.py
│
├── .env
├── Dockerfile
├── requirements.txt
├── docker-compose.yml



Here's a brief explanation of each folder:

  • app/: The main application package.

  • main.py: Entry point for the FastAPI application.

  • api/: Contains API-specific code.

  • v1/ and v2/: API versioning.

  • endpoints/: Individual endpoint implementations.

  • core/: Core application functionality.

  • config.py: Configuration settings.

  • security.py: Security-related functions.

  • database.py: Database connection setup.

  • dependencies/: Dependency-related code.

  • authentication.py: Authentication dependencies.

  • models/: Pydantic models or SQLAlchemy models.

  • tests/: Test cases for the application.

  • utils/: Utility functions.

  • main.py: The main entry point for running the FastAPI application.

  • .env: Environment variables configuration.

  • Dockerfile: Docker configuration for containerization.

  • requirements.txt: List of Python dependencies.

  • docker-compose.yml: Docker Compose configuration for running the app in a container.


This structure is just a suggestion, and you can adapt it based on your specific requirements and preferences. The key is to maintain clarity and organization as your FastAPI application grows.




Comments


bottom of page