Skip to content

Add @endpoint decorator for ModelAdmin custom FastAPI routes #49

Description

@borhanst

Overview

Create a new @endpoint decorator for ModelAdmin classes that adds custom FastAPI routes with full configuration support, similar to the existing @action decorator but for arbitrary API endpoints.

Problem

Currently, there's no standardized way to add custom FastAPI endpoints to the admin panel beyond the @action decorator (which only supports list/row/detail actions) or manually adding routes. Users need a decorator that can register arbitrary @app.router.add_api_route() endpoints on registered models.

Use Cases

  1. Custom health check endpoints
  2. Custom export/import APIs
  3. Custom status/monitoring endpoints
  4. Any arbitrary JSON API endpoint within the admin prefix

Proposed Solution

Implement an @endpoint decorator in fastapi_admin_kit/admin/decorators.py that:

  • Stores endpoint configuration on the decorated method as _admin_endpoint attribute
  • Automatically discovers and registers routes in build_model_router() via router.add_api_route()
  • Supports all FastAPI add_api_route parameters: path, methods, tags, dependencies, status_code, response_model, etc.
  • Integrates with existing RBAC via FastAPI dependencies
  • Is fully backward compatible

Decorator API

from fastapi_admin_kit.admin.decorators import endpoint

class ProductAdmin(ModelAdmin):
    @endpoint(
        path="/health-check",
        methods=["GET"],
        tags=["monitoring"],
        description="Health check endpoint",
        permission="can_view",
    )
    async def health_check(self, request):
        return {"status": "healthy"}

Supported Parameters

Param Type Required Default
path str Yes -
methods list[str] No ["GET"]
tags list[str] No []
description str No ""
name str No auto-generated
dependencies list[Any] No []
status_code int No 200
response_model Type[BaseModel] No None
summary str No ""
response_description str No ""

Router Changes

Modify fastapi_admin_kit/router.py::build_model_router() to iterate admin methods, check for _admin_endpoint, and register routes.

Backward Compatibility

  • Fully additive - doesn't break existing @action, @column, etc.
  • New endpoints opt-in via decoration
  • Existing models/routes continue to work unchanged

Example Usage

# Simple endpoint
@endpoint(path="/health", methods=["GET"])
async def health_check(self, request):
    return {"status": "ok"}

# With permissions
@endpoint(
    path="/stats",
    dependencies=[Depends(require_permission("products", "view"))],
)
async def stats(self, request):
    pass

Labels

enhancement, feature-request, admin

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestfeatureNew feature or functionalitygood first issueGood for newcomers

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions