Skip to content

FastAPI Integration

Asterios Raptis edited this page Mar 27, 2026 · 2 revisions

FastAPI Integration

PluginForge can mount plugin routes onto a FastAPI application. This is an optional feature - install with:

pip install pluginforge[fastapi]

Mounting Routes

from fastapi import FastAPI
from pluginforge import PluginManager

app = FastAPI()
pm = PluginManager("config/app.yaml")
pm.discover_plugins()
pm.mount_routes(app)

Custom Prefix

The default prefix is /api. You can change it:

pm.mount_routes(app, prefix="/v2/api")

Plugin Routes

Plugins provide routes by overriding get_routes(). Plugins bring their own route structure via their routers:

from fastapi import APIRouter
from pluginforge import BasePlugin

class HelloPlugin(BasePlugin):
    name = "hello"

    def get_routes(self) -> list:
        router = APIRouter(prefix="/hello")

        @router.get("/greet")
        def greet():
            greeting = self.config.get("greeting", "Hello")
            return {"message": greeting}

        @router.get("/info")
        def info():
            return {"plugin": self.name, "version": self.version}

        return [router]

With default prefix, routes are available at:

GET /api/hello/greet  -> HelloPlugin.greet()
GET /api/hello/info   -> HelloPlugin.info()

Plugins control their own URL structure. An export plugin can define routes like /books/{book_id}/export/{format} and they will be mounted under the prefix:

GET /api/books/{book_id}/export/pdf

Health Endpoint

You can expose a health endpoint using pm.health_check():

@app.get("/api/health")
def health():
    return pm.health_check()

Response:

{
    "export": {"status": "ok", "formats": ["epub", "pdf"]},
    "grammar": {"status": "error", "error": "LanguageTool API unreachable"}
}

Error Handling

  • If fastapi is not installed, mount_routes() raises ImportError with installation instructions
  • If the app argument is not a FastAPI instance, raises TypeError
  • Plugins that return empty routes from get_routes() are silently skipped

Clone this wiki locally