FastAPI SDK
A powerful SDK for building FastAPI applications with built-in authentication, authorization, and CRUD operations.
Features
-
🔐 Authentication & Authorization
- JWT-based authentication
- Role-based access control
- Fine-grained permissions
- User claims management
-
🗄️ Database Operations
- Automatic CRUD operations
- Soft delete support
- Pagination
- Relationship handling
-
🛡️ Security
- Ownership-based access control
- Permission-based authorization
- Role-based access control
- Secure token handling
-
📝 Type Safety
- Pydantic model integration
- Type hints throughout
- Automatic validation
- OpenAPI documentation
Documentation
Controllers & schemas
- Route Controller — Authenticated CRUD routes, pagination, and soft delete
- Model Controller — MongoDB/ODMantic CRUD, relationships, and ownership rules
- Schemas — Base, create, update, response, and paginated schema patterns
Auth & security
- Middlewares — JWT auth middleware setup, public routes, and claims helpers
- Auth Middleware Errors — Authentication error codes and response shapes
- Custom Permissions — Layered token + business-logic permission checks
- Encryption Key Mixin — Encrypted secret keys for shareable/public resource access
Responses & integrations
- Response Format — Standard JSON response and error envelope
- Webhooks — Signed webhook endpoints, replay protection, and event handlers
Quick Start
Using UV, start a new project from within the folder you wish to create it.
The project requires Python 3.13 or above.
uv init
uv venv
source .venv/bin/activaye
uv add fastapi-sdk
In order to test authentication, we need a set of asymmetric encryption keys for test access tokens. Generate the two keys using the command below:
openssl genrsa -des3 -out test_private_encrypted.pem 2048
openssl rsa -pubout -in test_private_encrypted.pem -out test_public_key.pem
openssl rsa -in test_private_encrypted.pem -out test_private_key.pem -outform PEM
This will create three keys:
test_private_encrypted.pem(keep safe somewhere)test_private_key.pemtest_public_key.pem
We will use the test_private_key.pem key to encrypt our JWT tokens and test_public_key.pem to decrypt them (asymmetric encryption).
Those test keys can added to the repository for ease of setup and running CI/CD testing.
Add the value of each key to your environment:
# .env.local
export TEST_PRIVATE_KEY_PATH="test_private_key.pem"
export TEST_PUBLIC_KEY_PATH="test_public_key.pem"
For the API to work in the real world, you will need settings for Fauthy authentication. Create a new tenant and use the client_id which will be required to get the public JWKs.
# .env.local
export AUTH_ISSUER="https://fauthy.com"
export AUTH_CLIENT_ID="test_client_id" # Replace with real client_id for production
Then some api settings:
# .env.local
export PUBLIC_ROUTES="/public/*,/other"
export ENVIRONMENT="development" # Replace with production when deploying to live server
A full .env.local complete included the MongoDB connection string:
export AUTH_PRIVATE_KEY="test_private_key.pem"
export AUTH_PUBLIC_KEY="test_public_key.pem"
export AUTH_ISSUER="https://fauthy.com"
export AUTH_CLIENT_ID="test_client_id" # Replace with real client_id for production
export PUBLIC_ROUTES="/public/*,/other"
export ENVIRONMENT="development" # Replace with production when deploying to live server
export MONGO_DATABASE_URI="mongodb://localhost:27017"
export MONGO_DATABASE_NAME="fastapi_sdk_test"
from fastapi import FastAPI
from fastapi_sdk.controllers.route import RouteController
from fastapi_sdk.middleware.auth import AuthMiddleware
from fastapi_sdk.controllers import ModelController
from fastapi_sdk.controllers.model import OwnershipRule
from tests.models import AccountModel
from tests.schemas import (
AccountCreate,
AccountResponse,
AccountUpdate,
)
class Account(ModelController):
"""Account controller."""
model = AccountModel
schema_create = AccountCreate
schema_update = AccountUpdate
schema_response = AccountResponse
cascade_delete = True # Will delete related projects and tasks
ownership_rule = OwnershipRule(
claim_field="account_id",
model_field="uuid",
allow_public=False,
)
relationships = {
"projects": {
"type": "one_to_many",
"controller": "Project",
"foreign_key": "account_id",
}
}
app = FastAPI()
# Add authentication middleware
app.add_middleware(
AuthMiddleware,
public_routes=["/docs", "/openapi.json"], # Routes that don't require authentication
auth_issuer="https://your-auth-server.com", # The issuer of the JWT tokens
auth_client_id="your-client-id", # Your application's client ID
env="prod", # Environment: "test" or "prod"
# Optional: Test environment keys
test_private_key_path="path/to/private.key", # Only needed for test environment
test_public_key_path="path/to/public.key", # Only needed for test environment
)
# Create a route controller
account_routes = RouteController(
prefix="/accounts",
tags=["accounts"],
controller=AccountController,
get_db=get_db,
schema_response=AccountResponse,
schema_response_paginated=BaseResponsePaginated[AccountResponse],
schema_create=AccountCreate,
schema_update=AccountUpdate,
)
# Include routes
app.include_router(account_routes.router)
Development
Setup
- Clone the repository
- Install dependencies:
uv sync
Running Tests
pytest
License
MIT License — see LICENSE for details
Metadata
Release files for fastapi-sdk 0.12.14
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_sdk-0.12.14.tar.gz | 35.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_sdk-0.12.14-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 73.8 kB
Release files / fastapi_sdk-0.12.14.tar.gz
| Download URL | fastapi_sdk-0.12.14.tar.gz |
|---|---|
| Size | 35.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a5aae2e90a8ea5c9b529b2d5a55506ae9351853fc91dc9a640e32f3ba48a7d2c
|
|
BLAKE2b-256 checksum How to use checksums |
816d137992e68dbe500d48476d2b4888b7dcce4731541d15aad9dc44907d2322
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.
Transparency logRelease files / fastapi_sdk-0.12.14-py3-none-any.whl
| Download URL | fastapi_sdk-0.12.14-py3-none-any.whl |
|---|---|
| Size | 38.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b8a8a41b89b728a92703fc2cbd22841c1d5eea6bf20bb92ab92a5fcf59f53fb
|
|
BLAKE2b-256 checksum How to use checksums |
ca469368445b24254e038425ce18f5384f989afdcdad5123f3883cd8e464bf18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.
Transparency log