Skip to main content

FastAPI-like router for Starlette with msgspec integration

Project description

starlette-msgspec

A FastAPI-like router for Starlette with msgspec integration for automatic request validation and OpenAPI documentation.

Installation

# Install with pip
pip install starlette-msgspec

# Or with uv
uv pip install starlette-msgspec

# For development on this library
uv sync --dev

Usage

from starlette.applications import Starlette
from starlette_msgspec import MsgspecRouter, add_openapi_routes
import msgspec
from typing import List


# Define your data model with msgspec
class Item(msgspec.Struct):
    name: str
    description: str = ""
    price: float
    tax: float = 0.0


app = Starlette()
router = MsgspecRouter()


@router.post("/items", tags=["items"])
async def create_item(body: Item):
    return body


@router.get("/items", tags=["items"])
async def get_items() -> List[Item]:
    # ... implementation
    return [Item(name="Example", price=10.5)]


# Include the router and add OpenAPI documentation routes
router.register_routes(app)
add_openapi_routes(app)

💡 For a more comprehensive example showing multiple routers, different data models, and advanced features, check out examples/basic_app.py.

Features

  • FastAPI-like router with method, path, and tags
  • Automatic request validation based on msgspec types
  • OpenAPI documentation generation using msgspec's built-in schema capabilities
  • Type annotations for request body and response

Running the example

# Install dependencies
uv sync --dev

# Run the example
uv run examples/basic_app.py

Then visit http://localhost:8000/docs to see the Swagger UI documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_msgspec-0.4.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_msgspec-0.4.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file starlette_msgspec-0.4.0.tar.gz.

File metadata

  • Download URL: starlette_msgspec-0.4.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.15

File hashes

Hashes for starlette_msgspec-0.4.0.tar.gz
Algorithm Hash digest
SHA256 471a7f818e987b6c4f607170fe5b4766e44456e6dbbdf93d7d7ba9ab33451191
MD5 d4ef067c22c6aa1b071c8dbe5ef1f556
BLAKE2b-256 90a1011944c572c4c4541471f547b14bc760d0fb83e30687425c5694d3209d93

See more details on using hashes here.

File details

Details for the file starlette_msgspec-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_msgspec-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 427d055ed49a115610607414951505dae39d1e9330675073b5384d64109d9b0e
MD5 f3d4f8368591211abdb1dbbc73d4cf9c
BLAKE2b-256 250acbdaa1a2d9029a2b6d9f3d0ac312668ac4136167805230e4b67c57b088ac

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page