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:http://localhost:8000/v1/users- Version 1 endpointhttp://localhost:8000/v1/docs- Version 1 documentationhttp://localhost:8000/v2/users- Version 2 endpointhttp://localhost:8000/v2/docs- Version 2 documentation
2. Microservices Aggregation
Combine multiple service-like modules:/users/docs, /products/docs, and /orders/docs.
3. Admin vs. Public APIs
Separate administrative and public interfaces: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
UseAPIRouter when you want:
- Shared OpenAPI schema
- Shared documentation
- Route organization within the same application
- Shared middleware and dependencies
/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
/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 theroot_path from the ASGI specification:
- Sets
root_path="/api/v1"for the sub-application - Updates the sub-application’s OpenAPI schema to include the prefix
- 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
Performance consideration: Sub-applications have minimal overhead. The main cost is routing to find the correct sub-application.
Comparison Summary
See Also
- Bigger Applications - Using routers
- Behind a Proxy - Understanding root_path
- Sub Applications - FastAPI docs