Skip to main content

Collection of middleware for Starlette applications

Project description

starlette-middleware-collection

starlette-middleware-collection on pypi CI and Publish Code Size

A small collection of Starlette compatible middleware, with FastAPI compatibility and examples.

Included Middleware

  • SizeLimit: Rejects requests with bodies larger than a configured byte limit.
  • RequestUUID: Adds a per-request UUID to request.state.id.

Installation

Install from PyPI:

pip install starlette-middleware-collection

Or with uv:

uv add starlette-middleware-collection

SizeLimit

The SizeLimit middleware checks the incoming content-length header and returns 413 Content Too Large when the request body exceeds the configured limit.

  • Constructor argument: limit (bytes)
  • Environment variable: MW_REQUEST_BODY_LIMIT
  • Default: 10 * 1024 * 1024 (10 MB)

Basic Usage

from fastapi import FastAPI
from starlette_middleware_collection import SizeLimit

app = FastAPI()
app.add_middleware(SizeLimit)

With a Custom Limit

from fastapi import FastAPI
from starlette_middleware_collection import SizeLimit

app = FastAPI()
app.add_middleware(SizeLimit, limit=5 * 1024 * 1024)  # 5 MB

Using Environment Variable (SizeLimit)

import os
from fastapi import FastAPI
from starlette_middleware_collection import SizeLimit

os.environ["MW_REQUEST_BODY_LIMIT"] = str(1024 * 1024)  # 1 MB

app = FastAPI()
app.add_middleware(SizeLimit)

[!NOTE] The constructor argument takes precedence over the environment variable.

RequestUUID

The RequestUUID middleware sets request.state.id for every request.

  • Constructor argument: uuid_version
  • Environment variable: MW_UUID_VERSION
  • Supported versions: 4, 7
  • Default: 7

Basic Usage (Default UUIDv7)

from fastapi import FastAPI, Request
from starlette_middleware_collection import RequestUUID

app = FastAPI()
app.add_middleware(RequestUUID)


@app.post("/")
async def read_root(request: Request):
    return {"uuid": str(request.state.id)}

With a Custom UUID Version

from fastapi import FastAPI
from starlette_middleware_collection import RequestUUID

app = FastAPI()
app.add_middleware(RequestUUID, uuid_version=4)

Using Environment Variable (RequestUUID)

import os
from fastapi import FastAPI
from starlette_middleware_collection import RequestUUID

os.environ["MW_UUID_VERSION"] = "4"

app = FastAPI()
app.add_middleware(RequestUUID)

[!NOTE] The constructor argument takes precedence over the environment variable.

If an unsupported uuid_version is provided, a ValueError is raised.

Combined Example

from fastapi import FastAPI, Request
from starlette_middleware_collection import RequestUUID, SizeLimit

app = FastAPI()
app.add_middleware(SizeLimit, limit=2 * 1024 * 1024)  # 2 MB
app.add_middleware(RequestUUID, uuid_version=7)


@app.post("/upload")
async def upload(request: Request):
    return {"request_id": str(request.state.id)}

Requirements

starlette-middleware-collection

Currently supports only Python 3.14 with latest version of Starlette. We plan on supporting older versions of Python and Starlette in the near future.

Contributing

Open Issues Closed Issues Open Pull Requests Closed Pull Requests

For bugs / feature requests please submit issues. If you would like to contribute to this project, you are welcome to submit a pull request

Warranty / Liability / Official support

This project is being developed independently, we provide the package "as-is" without any implied warranty or liability, usage is your own responsibility

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_middleware_collection-0.1.2.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file starlette_middleware_collection-0.1.2.tar.gz.

File metadata

  • Download URL: starlette_middleware_collection-0.1.2.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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":true}

File hashes

Hashes for starlette_middleware_collection-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2f981682adf2fae9111a80d9fc2ae4ea7777c2b14fc771958d55432e43fddf67
MD5 e19ef7bd358cc78ceec490eb3edcb597
BLAKE2b-256 4038bc365027cc162f1d95e751e1be1335a8df4a37ff5a0661d372da56948806

See more details on using hashes here.

File details

Details for the file starlette_middleware_collection-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: starlette_middleware_collection-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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":true}

File hashes

Hashes for starlette_middleware_collection-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7d237598f051e2b714bab0c2c9d0c3166d3c6eb5e6da5af936131e55fe489c20
MD5 98173802d7584bbd6ac9525a03310eaa
BLAKE2b-256 dd59d6850a1b811d3065b5436ee8598c87bcfddfaced7f69aa9564acdd694c7b

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