Skip to content
tezvyn:

FastAPI: Mounting Independent Sub-Applications

Source: fastapi.tiangolo.comHardHow cards are made

FastAPI: Mounting Independent Sub-Applications

Mounting delegates a URL prefix to a separate FastAPI app, giving it its own isolated logic and API docs. Use it to combine microservices or isolate domains. The footgun: the main app's dependencies and middleware do not apply to the sub-app.

Why it exists

Mounting solves the problem of composing multiple, fully independent web services into a single, unified API. Instead of running separate services on different ports or domains, you can present them as different path prefixes under one host, simplifying deployment, routing, and client interaction.

The mental model

Think of mounting like plugging a self-contained power strip into a wall outlet. The wall outlet (the main app) provides a connection point at a specific path (e.g., /sub-app), but the power strip (the sub-app) manages all its connected devices independently. The sub-app has its own circuit breaker (exception handlers) and features (API docs), and it doesn't know or care what else is happening in the main application.

How it works

You create two or more FastAPI application instances. In your main app, you call the .mount() method, providing a path prefix as the first argument (e.g., "/sub") and the sub-application object as the second. FastAPI then directs any incoming request starting with that prefix to the sub-application. The sub-app processes the request as if the prefix didn't exist, using its own routing, dependencies, and middleware. Each app generates its own OpenAPI schema, resulting in separate API docs (e.g., /docs for the main app and /sub/docs for the sub-app).

When to use it

Use mounting when you need strong isolation between parts of your application. It's perfect for a microservices-style architecture where a gateway app mounts several backend services. It's also useful in a "modular monolith" to enforce strict boundaries between different business domains, preventing them from sharing code or state implicitly.

When not to use it

Don't use mounting if you need to share dependencies, middleware, or configuration between application components. If your components are tightly coupled and need to interact with a shared state or use common utility functions via dependency injection, using an APIRouter and including it in the main app is the correct pattern. Mounting creates isolation, which is counterproductive in that scenario.

One canonical example

Imagine a main application that also needs to serve a completely separate admin panel API. You would define the main app and the admin app as two distinct FastAPI instances. Then, in your main application file, you would mount the admin app:

from fastapi import FastAPI

from admin_panel import admin_app # admin_app is a separate FastAPI() instance

app = FastAPI()

@app.get("/users/me")

def read_current_user():
return {"user_id": "the_current_user"}
app.mount("/admin", admin_app)

A request to /users/me is handled by the main app. A request to /admin/dashboard is routed to the admin_app instance. The API docs would be available at /docs (main app) and /admin/docs (admin app).

Interview question

When developing a FastAPI application, which scenario would make app.mount() an inappropriate choice?

  • a.Integrating a third-party API that has its own independent routing and documentation.
  • b.Combining several distinct microservices into a unified API gateway.
  • c.Ensuring all sub-components consistently apply a shared dependency injection pattern and global middleware.Correct
  • d.Structuring a modular monolith with strict boundaries between business domains.
Why?

The card explicitly states, "Don't use mounting if you need to share dependencies, middleware, or configuration between application components." Mounting isolates sub-applications, preventing them from inheriting the main app's shared concerns.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles