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.
Read the documentation for the tutorial, handler lifecycle, backends, querying, schemas, and OpenAPI.
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
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 |
| Writing a backend | Add support for another storage system |
| 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 home 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.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| muffin_rest-15.3.0.tar.gz | 29.0 kB | Without provenance | Details for muffin_rest-15.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| muffin_rest-15.3.0-py3-none-any.whl | Python 3 | none | any | Details for muffin_rest-15.3.0-py3-none-any.whl |
Total release size: 68.7 kB
Release files / muffin_rest-15.3.0.tar.gz
| Download URL | muffin_rest-15.3.0.tar.gz |
|---|---|
| Size | 29.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6d51309ef678517fc45124b8ae1e014a81b286d9b85b813575402cf10feb908b
|
|
BLAKE2b-256 checksum How to use checksums |
4bf27d51d0290f533aec1bd0f317f060d70e83c3a7f977f3671cab4ab9c364f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","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.3.0-py3-none-any.whl
| Download URL | muffin_rest-15.3.0-py3-none-any.whl |
|---|---|
| Size | 39.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dcbb03d41d8597b9fdfe25af1929451b730ce1b793ce1e48638ea650aa41b2e1
|
|
BLAKE2b-256 checksum How to use checksums |
032346430c96a9c6f40381b0a80d353b4d47ccbecaafc50e51e7af9e667f0bf6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","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.3.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.