-
Notifications
You must be signed in to change notification settings - Fork 0
FastAPI Integration
Asterios Raptis edited this page Mar 27, 2026
·
2 revisions
PluginForge can mount plugin routes onto a FastAPI application. This is an optional feature - install with:
pip install pluginforge[fastapi]from fastapi import FastAPI
from pluginforge import PluginManager
app = FastAPI()
pm = PluginManager("config/app.yaml")
pm.discover_plugins()
pm.mount_routes(app)The default prefix is /api. You can change it:
pm.mount_routes(app, prefix="/v2/api")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
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"}
}- If
fastapiis not installed,mount_routes()raisesImportErrorwith installation instructions - If the
appargument is not a FastAPI instance, raisesTypeError - Plugins that return empty routes from
get_routes()are silently skipped