Skip to main content

A FastAPI admin SDK for building admin interfaces with resource management, permissions, and CRUD operations

Project description

FastAPI Admin SDK

Coverage PyPI version Python 3.11+ Tests

A FastAPI admin SDK for building admin interfaces with resource management, permissions, and CRUD operations.

Installation

Install the package using pip:

pip install fastapi-admin-sdk

Or using uv:

uv add fastapi-admin-sdk

Quick Start

1. Configure Settings

Set up your environment variables:

export ADMIN_DB_URL="postgresql+asyncpg://user:password@localhost/dbname"
export ORM_TYPE="sqlalchemy"

Or create a .env file:

ADMIN_DB_URL=postgresql+asyncpg://user:password@localhost/dbname
ORM_TYPE=sqlalchemy

2. Define Your Model

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase):
    pass

class User(Base):
    __tablename__ = "users"
    
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]
    email: Mapped[str]

3. Create a Resource

from fastapi_admin_sdk import Resource, SQLAlchemyResource
from fastapi_admin_sdk.db import get_session_factory

session_factory = get_session_factory()
user_resource = SQLAlchemyResource(
    model=User,
    session_factory=session_factory,
    lookup_field="id"
)

4. Create an Admin Class

from fastapi import Request
from pydantic import BaseModel
from fastapi_admin_sdk import BaseAdmin, register

class UserCreateSchema(BaseModel):
    name: str
    email: str

class UserUpdateSchema(BaseModel):
    name: str | None = None
    email: str | None = None

@register(user_resource)
class UserAdmin(BaseAdmin):
    list_display = ["id", "name", "email"]
    list_filter = ["name"]
    search_fields = ["name", "email"]
    ordering = ["id"]
    
    create_form_schema = UserCreateSchema
    update_form_schema = UserUpdateSchema
    lookup_field = "id"
    
    async def has_create_permission(self, request: Request):
        # Add your permission logic here
        return True

5. Add Routes to Your FastAPI App

from fastapi import FastAPI
from fastapi_admin_sdk import router

app = FastAPI()
app.include_router(router, prefix="/admin")

Features

  • Resource Management: Define resources with CRUD operations
  • Permission System: Customizable permission checks for create, read, update, delete, and list operations
  • Filtering & Pagination: Built-in support for filtering and pagination
  • SQLAlchemy Support: First-class support for SQLAlchemy async models
  • Type Safety: Built with Pydantic for data validation

API Endpoints

Once you've registered your admin classes and included the router, the following endpoints will be available:

  • POST /admin/{resource_name}/create - Create a new resource instance
  • GET /admin/{resource_name}/list - List resource instances with filtering and pagination
  • GET /admin/{resource_name}/{lookup}/retrieve - Retrieve a specific resource instance
  • PATCH /admin/{resource_name}/{lookup}/update - Update an existing resource instance
  • DELETE /admin/{resource_name}/{lookup}/delete - Delete a resource instance

Development

Running Tests

# Install dev dependencies
uv sync --all-groups --all-extras

# Run tests
uv run pytest

# Run tests with coverage
uv run pytest --cov=fastapi_admin_sdk --cov-report=term

Building the Package

python -m build

Publishing to PyPI

The package is automatically published to PyPI when a git tag is pushed. Make sure to set up the PYPI_API_TOKEN secret in your GitHub repository.

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_admin_sdk-0.1.0.tar.gz (114.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_admin_sdk-0.1.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_admin_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_admin_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 114.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_admin_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6051945dbf4d930664e9c5d44baad7818b91c443bd7934f97c3c85d1fc8f8f42
MD5 2f380bd66c82e4891fa8b10a0c926578
BLAKE2b-256 503a12916fc3165358d24ba4378a464d0190d72a57d37031b34a27eec058c67b

See more details on using hashes here.

File details

Details for the file fastapi_admin_sdk-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_admin_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04eb272d1a6bd107aaf78d502a99fb5708130c29c572f300b31f3fb5461e5563
MD5 19a28765397049e9959bafcce9caf911
BLAKE2b-256 9824f7a202b9326d1651f4ae52a23b6c0bcaa162ab8f7b48ec05e6f9bb8b0cdd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page