Skip to main content

Sanic RESTful Framework (SRF)

A RESTful API framework based on Sanic, providing a Django REST Framework-like development experience to help you quickly build high-performance Web APIs.

Features

  • 🚀 Base ViewSets - Complete CRUD operations (Create, Retrieve, Update, Destroy, List).
  • 🔒 Pydantic Integration - Data validation and serialization using Pydantic.
  • 🔍 Powerful Filtering System - Support for search, JSON logic filtering, query parameter filtering, and ordering.
  • 📄 Pagination - Built-in pagination handler with customizable pagination parameters.
  • 🛡️ Authentication & Permissions - JWT-based authentication system with permission control, and provides third-party authentication integration, such as GitHub login.
  • ⚡ Asynchronous ORM - Built-in support for asynchronous database queries using Tortoise ORM, and more.
  • 💓 Health Checks - Built-in health check routes supporting multiple service checks.
  • 🎯 Auto Route Registration - Intelligent route registration with support for custom actions.
  • 🔄 Rate Limiting - Built-in rate limiting middleware to control request rates.

Requirements

  • Python >= 3.11
  • Sanic >= 25.0.0
  • Tortoise ORM >= 1.1.8
  • Pydantic >= 2.6.1

Installation

Install from Source

# Recommended installation method
pip install sanic-restful-framework

# Install production dependencies
pip install -e .

# Install development dependencies
pip install -e ".[dev]"

Quick Start

To get started with SRF, follow these steps:

  1. Define your models using Tortoise ORM. Create your database models that inherit from tortoise.models.Model and define the necessary fields.

  2. Create Pydantic schemas for data validation and serialization. Define separate schemas for reading (output) and writing (input) operations.

  3. Create a ViewSet by inheriting from BaseViewSet. Configure the model, search fields, and filter fields. Implement the queryset property to return your model's queryset, and optionally override get_schema() to return different schemas based on the current action.

  4. Register routes using SanicRouter. Create a router instance with an optional prefix, register your ViewSet with a path, and add the router's blueprint to your Sanic application.

  5. Run your application using Sanic's built-in server or your preferred ASGI server.

Core Features

ViewSets

BaseViewSet provides standard CRUD operations:

  • list() - GET /resource - Retrieve a list of resources
  • create() - POST /resource - Create a new resource
  • retrieve() - GET /resource/ - Retrieve a single resource
  • update() - PUT/PATCH /resource/ - Update a resource
  • destroy() - DELETE /resource/ - Delete a resource
from srf.views import BaseViewSet
from tortoise.queryset import QuerySet
from .models import Project
from .schema import ProjectSchemaReader

class ProjectViewSet(BaseViewSet):
    def get_schema(self):
        return ProjectSchemaReader

    @property
    def queryset(self) -> QuerySet:
        return Project.all()

Congrats! At this point, you have completed a basic CRUD (Create, Read, Update, Delete) interface, just like in Django REST Framework.

Custom Actions

Use the @action decorator to define custom actions:

from srf.views import action
from sanic.response import JSONResponse

class ProjectViewSet(BaseViewSet):
    @action(detail=False, url_path="featured", methods=["get"])
    async def list_featured(self, request):
        projects = self.queryset.filter(is_featured=True)
        schema = self.get_schema()
        data = [schema.model_validate(p, from_attributes=True).model_dump(by_alias=True) for p in await projects]
        return JSONResponse(data)

    @action(detail=True, url_path="archive", methods=["post"])
    async def archive(self, request, pk):
        project = await self.get_object(pk)
        project.is_archived = True
        await project.save()
        return JSONResponse({"status": "archived"})

Filters

The framework includes multiple built-in filters:

1. Search Filter (SearchFilter)

# GET /projects?search=api
# Searches for "api" in fields specified by search_fields

2. JSON Logic Filter (JsonLogicFilter)

# GET /projects?filter={"and":[{"==":[{"var":"is_active"},true]},{"like":[{"var":"name"},"api"]}]}

3. Query Parameter Filter (QueryParamFilter)

# GET /projects?name=myproject&is_active=true

4. Ordering Filter (OrderingFactory)

# GET /projects?ordering=name,-created_date
# Orders by name ascending, created_date descending

Pagination

Pagination automatically reads page and page_size from query parameters:

# GET /projects?page=1&page_size=20

Response format:

{
  "count": 100,
  "next": true,
  "previous": false,
  "results": [...]
}

Authentication & Permissions

Authentication Middleware

JWT authentication middleware. Requests without a token get AnonymousUser. See the Authentication Middleware.

from srf.middleware.authmiddleware import set_user_to_request_ctx

app.middleware("request")(set_user_to_request_ctx)

Permission Classes

from srf.permission.permission import IsAuthenticated, SafeMethodOnly, SuperuserOnly

class ProjectViewSet(BaseViewSet):
    permission_classes = (IsAuthenticated,)  # Requires authentication
    # or
    permission_classes = (SuperuserOnly,)  # Requires a superuser
    # or read-only: allow GET/HEAD/OPTIONS only
    permission_classes = (SafeMethodOnly,)

Health Checks

The framework provides a health check view that runs a configurable list of checks:

from srf.health.checks import RedisCheck, SQLiteCheck
from srf.health.viewset import health_check

app.config.HEALTH_CHECK_LIST = [RedisCheck, SQLiteCheck]  # required
app.add_route(health_check, "/health", methods=["GET"])

Each check is a class that is instantiated with the Sanic app and exposes the required clients — e.g. the Redis check uses app.ctx.redis and the SQLite check uses app.ctx.sqlite. Attach those clients to app.ctx before startup. Access GET /health to check the status of the registered services.

Configuration

SRF reads configuration from srf.config.settings via settings (uppercase keys such as DEFAULT_FILTERS, USER_REGISTER_EMAIL_CODE). Override via Sanic app.config after settings.set_app(app) (or pass config when creating the app). srfconfig is a deprecated alias.

from srf.config import settings
from srf.config.settings import DEFAULT_FILTERS

app.config.JWT_SECRET = "your-secret-key"  # required for JWT / register_all_auth_urls
app.config.DEFAULT_FILTERS = DEFAULT_FILTERS
app.config.REQUEST_LIMITERS = []
settings.set_app(app)

For auth email verification and social login, ensure Redis is on app.ctx.redis. Email codes use the USER_REGISTER_EMAIL_CODE key template (default "USER_REGISTER:EMAIL_CODE:{email}"). Social one-time codes use SOCIAL_LOGIN_REDIS_CODE_PREFIX (default "social-login").

Dependencies

Main dependencies:

  • sanic>=25.0.0 - Async web framework
  • pydantic>=2.6.1 - Data validation
  • tortoise-orm>=1.1.8 - ORM
  • sanic-jwt>=1.8.0 - JWT authentication

See pyproject.toml for the complete dependency list.

Development

Install Development Dependencies

Development dependencies (pytest, black, isort, ruff, ...) are optional and are not installed by default:

# Production/runtime dependencies only (default)
uv sync

# Everything needed for local development
uv sync --extra dev

Or with pip:

pip install -e ".[dev]"

License

MIT License

Author

Name Email
Chacer 1364707405c@gmail.com

Contributing

Issues and Pull Requests are welcome!

Want to join? Email us!

Metadata

Release files for sanic-restful-framework 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-restful-framework 0.1.3
File Size Uploaded Provenance Actions
sanic_restful_framework-0.1.3.tar.gz 78.6 kB Without provenance Details for sanic_restful_framework-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sanic-restful-framework 0.1.3
File Interpreter ABI Platform Actions
sanic_restful_framework-0.1.3-py3-none-any.whl Python 3 none any Details for sanic_restful_framework-0.1.3-py3-none-any.whl

Total release size: 133.7 kB

Release files / sanic_restful_framework-0.1.3.tar.gz

Download URL sanic_restful_framework-0.1.3.tar.gz
Size 78.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3bacf03f2a4bcf450d6e46b9d9f946f9bcb3140afb8da3fa1b927e5b78dfd4be
BLAKE2b-256 checksum
How to use checksums
e1daa8cca3ad0ec73f80b5ba5b2c39611d57f5eb879ff179a8f2aecb62dfdb1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / sanic_restful_framework-0.1.3-py3-none-any.whl

Download URL sanic_restful_framework-0.1.3-py3-none-any.whl
Size 55.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c37a3e194ff270e197e3016817da83fc07da48e46c711b3f7de4d96610e322df
BLAKE2b-256 checksum
How to use checksums
8d1a3560bc5a506f0b9edd9f32bf28ab1b1e28d7465c513837b45e2d66c54804
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for sanic-restful-framework 0.1.3

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page