Skip to main content

Muffin-REST

Muffin-REST turns Muffin handlers into REST resources. Describe a model or table in a handler's Meta and the handler serves list and detail endpoints with filtering, sorting, pagination, and an OpenAPI spec generated from the code. Peewee, SQLAlchemy Core, and MongoDB backends share the same handler API, so changing storage does not change your routes.

Tests Status PyPI Version Python Versions

Install

Python >= 3.11 and a Muffin application.

pip install muffin-rest                # core
pip install "muffin-rest[peewee]"      # Peewee ORM
pip install "muffin-rest[sqlalchemy]"  # SQLAlchemy Core
pip install "muffin-rest[yaml]"        # YAML docstrings for the OpenAPI spec

The Mongo backend needs muffin-mongo; it is not an extra of this package.

Quickstart

from muffin import Application
from muffin_rest import API
from muffin_rest.peewee import PWRESTHandler

from models import User

app = Application("myapp")
api = API(title="User Service", version="1.0")


@api.route
class Users(PWRESTHandler):
    class Meta:
        model = User
        filters = "name", "email"
        sorting = "name", ("created_at", {"default": "desc"})
        limit = 20


api.setup(app, prefix="/api")

api.route without a path derives the URLs from Meta.name, which defaults to the model's table name:

Method Path Action
GET /api/user list, with filter/sort/pagination
POST /api/user create one resource or a list of them
GET /api/user/{id} retrieve
PUT /api/user/{id} update
DELETE /api/user/{id} delete

Create returns 200 with the serialized resource. Delete returns 200 too, with the deleted id or ids for Peewee, and null for SQLAlchemy and Mongo. Handlers answer 405 for methods outside get, post, put, delete unless you add them to methods.

Backends

from muffin_rest.sqlalchemy import SARESTHandler

@api.route
class Pets(SARESTHandler):
    class Meta:
        table = pet_table
        database = db
from muffin_rest.mongo import MongoRESTHandler

@api.route
class Pets(MongoRESTHandler):
    class Meta:
        collection = mongo_collection
        limit = 20
        aggregate = [{"$match": {"status": "available"}}]  # used with pagination

The Peewee handler needs a peewee_aio model with a manager, or an explicit Meta.manager. Composite primary keys are supported; the route id joins the key columns with ::. The Mongo handler needs muffin-mongo.

Documentation

Published documentation: https://klen.github.io/muffin-rest/

The README covers the shape of the library. The guides explain the parts that need more than a snippet:

Guide What it answers
Tutorial Build and run a small API from scratch
Handlers and lifecycle What runs on each request, and which hook to override
Backends Peewee, SQLAlchemy, and Mongo options and differences
Querying where, sort, pagination, field selection
Schemas Generated schemas, validation, custom fields
OpenAPI Spec generation, docstrings, auth, response types
Errors APIError, validation errors, status codes
Cookbook Auth, scoping, soft delete, custom filters and routes
API reference Public classes, options, and hook signatures
Development Tests, linting, examples, release

Start with the documentation index if you are browsing the repository.

Development

uv sync                 # install dependencies
uv run pytest           # run tests
make lint               # pyrefly + ruff
make docs               # build the documentation
make docs-serve         # preview it locally

Mongo tests need Docker with a container named mongo (docker start mongo). Examples run against SQLite:

uv run uvicorn examples.peewee_orm:app --reload --port=5000
uv run uvicorn examples.sqlalchemy_core:app --reload --port=5000

See Development for the full workflow.

Bug tracker and contributing

Issues and pull requests: https://github.com/klen/muffin-rest

Contributors

  • klen (Kirill Klenov)

License

MIT. See LICENSE.

Metadata

Release files for muffin-rest 15.1.0

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

Source distribution (sdist)

Source distribution for muffin-rest 15.1.0
File Size Uploaded Provenance Actions
muffin_rest-15.1.0.tar.gz 28.3 kB Without provenance Details for muffin_rest-15.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for muffin-rest 15.1.0
File Interpreter ABI Platform Actions
muffin_rest-15.1.0-py3-none-any.whl Python 3 none any Details for muffin_rest-15.1.0-py3-none-any.whl

Total release size: 67.4 kB

Release files / muffin_rest-15.1.0.tar.gz

Download URL muffin_rest-15.1.0.tar.gz
Size 28.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e6cc675d7af3c3792af8b81562fd3f7f95fc4baec92fa363adf06c1b15134b80
BLAKE2b-256 checksum
How to use checksums
f48e6f219172378d68eb0a8dabd340d28c2866e030bc63fe1c9d72ecf820d139
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / muffin_rest-15.1.0-py3-none-any.whl

Download URL muffin_rest-15.1.0-py3-none-any.whl
Size 39.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e4a3373193f4411fa8bd887819e02dc2f1594c2e99a7dcf894fab099d5b2ed18
BLAKE2b-256 checksum
How to use checksums
2ae05f6da05fe69c06459ff454142a6532592987eaaf6559e8a1c1491402fef3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for muffin-rest 15.1.0

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

15.1.0 This release

2 release files No provenance

0.0.1

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