Skip to main content
FastAPI supports mounting independent sub-applications, enabling modular API architectures where each module has its own documentation, middleware, and dependencies.

What are Sub-Applications?

A sub-application is a complete, independent FastAPI application that’s mounted at a specific path within a parent application. Each sub-application:
  • Has its own OpenAPI schema
  • Has its own docs UI (accessible at /sub-path/docs)
  • Can have its own middleware
  • Can have its own exception handlers
  • Maintains independence from the parent application
This is different from using APIRouter, which shares the same OpenAPI schema and docs with the parent application.

Basic Sub-Application

Creating the Applications

Accessing the Applications

With the above setup:
  • Main app root: http://localhost:8000/ → Main app response
  • Main app docs: http://localhost:8000/docs → Main app OpenAPI only
  • Sub-app root: http://localhost:8000/subapi/ → Sub-app response
  • Sub-app items: http://localhost:8000/subapi/items → Items from sub-app
  • Sub-app docs: http://localhost:8000/subapi/docs → Sub-app OpenAPI only
Each application has completely independent documentation. Routes from the main app don’t appear in sub-app docs and vice versa.

Use Cases for Sub-Applications

1. API Versioning

Create separate sub-applications for different API versions:
Now you have:
  • http://localhost:8000/v1/users - Version 1 endpoint
  • http://localhost:8000/v1/docs - Version 1 documentation
  • http://localhost:8000/v2/users - Version 2 endpoint
  • http://localhost:8000/v2/docs - Version 2 documentation
This approach lets you maintain different API versions with completely separate schemas and documentation, making deprecation easier.

2. Microservices Aggregation

Combine multiple service-like modules:
Each service has its own documentation at /users/docs, /products/docs, and /orders/docs.

3. Admin vs. Public APIs

Separate administrative and public interfaces:
Mounting alone doesn’t provide security. You still need proper authentication and authorization in your sub-applications.

Sub-Application Configuration

Different Middleware

Each sub-application can have its own middleware:

Independent Dependencies

Each sub-application has its own dependency injection:
Dependency overrides are also independent. Overriding a dependency in one sub-application doesn’t affect others.

Different Exception Handlers

Working with Routers vs. Sub-Applications

When to Use APIRouter

Use APIRouter when you want:
  • Shared OpenAPI schema
  • Shared documentation
  • Route organization within the same application
  • Shared middleware and dependencies
All routes appear in one OpenAPI schema at /docs.

When to Use Sub-Applications

Use sub-applications when you want:
  • Independent OpenAPI schemas
  • Separate documentation
  • Complete isolation between modules
  • Different middleware or exception handling
  • API versioning
Each sub-application has its own docs at /users/docs and /products/docs.
Rule of thumb: If you need separate documentation, use sub-applications. If you just need route organization, use routers.

Technical Details: root_path

When you mount a sub-application, FastAPI automatically handles the root_path from the ASGI specification:
FastAPI automatically:
  1. Sets root_path="/api/v1" for the sub-application
  2. Updates the sub-application’s OpenAPI schema to include the prefix
  3. Adjusts the docs UI URLs to work correctly
This is handled automatically. You don’t need to manually configure root_path when mounting sub-applications.

Advanced Patterns

Conditional Sub-Application Mounting

Sub-Application Factory

Proxying to External Services

Combine local sub-applications with proxies to external services:

Best Practices

Use sub-applications for true independence: If modules need completely different configurations, use sub-applications.
Document the mount paths: Make it clear in your documentation where each sub-application is mounted.
Consider API versioning early: If you might need versioning, structure your app with sub-applications from the start.
Performance consideration: Sub-applications have minimal overhead. The main cost is routing to find the correct sub-application.
Don’t over-modularize: If you just need route organization, use APIRouter instead. Sub-applications add complexity.

Comparison Summary

See Also