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.

Read the documentation for the tutorial, handler lifecycle, backends, querying, schemas, and OpenAPI.

Documentation 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

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 17.0.1

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 17.0.1
File Size Uploaded Provenance Actions
muffin_rest-17.0.1.tar.gz 29.5 kB Without provenance Details for muffin_rest-17.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 70.6 kB

Release files / muffin_rest-17.0.1.tar.gz

Download URL muffin_rest-17.0.1.tar.gz
Size 29.5 kB
Tags Source
SHA-256 checksum
How to use checksums
704054dbe40a5452f6d6faada950b8901a2476f225ef3aa8d0d802b071809704
BLAKE2b-256 checksum
How to use checksums
3b3648f60bd058eb2121e00aae2f8a52ed42c7555968c42bc2661d8eb867310e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","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-17.0.1-py3-none-any.whl

Download URL muffin_rest-17.0.1-py3-none-any.whl
Size 41.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7bb773e7113b371956cde5ade06049bc9927e5b8d0989666300dddd7b78fc0bd
BLAKE2b-256 checksum
How to use checksums
59953ca67d36573f6e17053e43c20bf7adfda5bfc73d2a7270297e7773183477
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","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 17.0.1

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

17.0.1 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