Published on

FastAPI Project Structure: The Best Way to Scale in 2026

To structure a FastAPI project effectively, you should use a modular layout that separates your logic into dedicated folders for models, schemas, and routes. For a standard production-ready application in 2026, the most reliable approach is the "Package Structure," which places all application code inside a main app/ directory containing an __init__.py file. This setup allows your project to scale from a single file to thousands of lines of code while remaining easy to test and maintain.

What are the core components of a FastAPI project?

Before building the folders, you need to understand the different types of files that make up a modern API. FastAPI relies on specific Python tools to handle data and communication.

Models are Python classes that represent your database tables (the way data is stored permanently). Schemas (often called Pydantic models) define the data structure for inputs and outputs, ensuring that the information coming in from a user is valid. Routers act like a traffic controller, directing web requests to the correct piece of code based on the URL the user visits.

We’ve found that clearly separating these three areas prevents "spaghetti code," where everything is tangled together and hard to fix. By keeping them apart, you can change your database layout without accidentally breaking the way users send you data.

What does the standard folder tree look like?

A professional FastAPI project follows a predictable pattern. This helps other developers (and AI coding assistants) understand where to find specific logic.

You will start with a root folder (your project name). Inside that, you will have a subfolder named app that holds the actual code.

Here is the basic visual structure:

my_fastapi_project/
├── app/
│   ├── __init__.py
│   ├── main.py
│   ├── api/
│   ├── core/
│   ├── models/
│   └── schemas/
├── tests/
├── .env
├── .gitignore
└── requirements.txt

How do you set up the basic folder tree?

Setting up the folders correctly from the start prevents import errors later. You can do this through your file explorer or your terminal.

Step 1: Create the root directory. Open your terminal and create a folder for your project.

mkdir my_fastapi_project
cd my_fastapi_project

What you should see: A new, empty folder that will hold all your project files.

Step 2: Create the app package. Inside the project folder, create the app folder and an empty file named __init__.py. The __init__.py file tells Python that this folder should be treated as a package (a collection of code modules).

mkdir app
touch app/__init__.py

What you should see: A folder named app containing a single file with two underscores on each side of the name.

Step 3: Create the main entry point. Create a file named main.py inside the app folder. This is the file that will start your web server.

touch app/main.py

What goes inside the app sub-folders?

As your project grows, you don't want every piece of code sitting in main.py. You should create specific sub-folders inside app to keep things organized.

The api/ folder should contain your "routes" (the URL paths like /users or /items). The core/ folder is for "config" (settings like your secret keys or database URLs).

The models/ folder holds your database definitions, usually created with a tool like SQLModel or SQLAlchemy. The schemas/ folder holds your Pydantic classes, which validate the data users send to your API.

What tools do you need to get started?

To build this structure in 2026, you need a modern environment. Technology moves fast, so using the latest stable versions ensures you have the best security and speed.

What You'll Need:

  • Python 3.15+: The latest version of Python offers better performance and improved type hinting (a way to label what kind of data your code uses).
  • An AI-Native Code Editor: Tools like Cursor or VS Code with the latest "Copilot Next" extensions are recommended. These editors understand the FastAPI structure and can help you generate boilerplate code (repetitive code needed for setup).
  • FastAPI 0.115+: The framework itself, which handles the web requests.
  • Uvicorn: An ASGI (Asynchronous Server Gateway Interface) server that actually runs your FastAPI code so people can visit it in a browser.

How do you write the code for a structured main.py?

Once your folders are ready, you need to connect them. Instead of putting all your logic in main.py, you will "include" the routers from your other folders.

Here is a simple example of what your app/main.py should look like in this structure:

# app/main.py
from fastapi import FastAPI
# We import the router from our api folder (we will create this next)
from app.api import users

app = FastAPI(title="My Professional API")

# This connects the user-related code to the main app
app.include_router(users.router, prefix="/users", tags=["users"])

@app.get("/")
def read_root():
    # A simple message to show the API is working
    return {"message": "Welcome to the API!"}

Next, you would create app/api/users.py to handle the actual user logic:

# app/api/users.py
from fastapi import APIRouter

# APIRouter allows us to group related paths together
router = APIRouter()

@router.get("/")
def get_users():
    # In a real app, this would fetch data from a database
    return [{"username": "dev_user1"}, {"username": "dev_user2"}]

What are the common structure mistakes for beginners?

It is normal to feel overwhelmed by all these folders when you are just starting. Many beginners make the mistake of putting everything into one giant file.

One common "gotcha" is the Circular Import. This happens when File A tries to import File B, while File B is also trying to import File A. Python gets confused and crashes. By using the folder structure described above and keeping your database models separate from your API routes, you can avoid this entirely.

Another mistake is forgetting the .env file. Never put passwords or secret keys directly in your code. Put them in a .env file (a simple text file for settings) and add that file to your .gitignore so it doesn't get uploaded to the internet.

Next Steps

Now that you have a clean structure, you are ready to start building real features. Focus on creating one "resource" at a time, such as "Products" or "Profiles," and give each one its own file in the models, schemas, and api folders.

Once you are comfortable with this layout, you might want to explore Dependency Injection (a way to share tools like database connections across different parts of your app). You can also look into automated testing using Pytest to make sure your code doesn't break when you add new features.

For more detailed guides, visit the official FastAPI documentation.


Read the Structure Documentation