# starlette-compress
[](https://pypi.org/project/starlette-compress)
[](https://liberapay.com/Zaczero/)
[](https://github.com/sponsors/Zaczero)
**starlette-compress** is a fast and simple middleware for compressing responses in [Starlette](https://www.starlette.io). It supports more compression algorithms than Starlette's built-in GZipMiddleware, and has more sensible defaults.
- Python 3.9+ support
- Compatible with `asyncio` and `trio` backends
- ZStd, Brotli, and GZip compression
- Sensible default configuration
- [Zero-Clause BSD](https://choosealicense.com/licenses/0bsd/) — public domain dedication
- [Semantic Versioning](https://semver.org) compliance
## Installation
```sh
pip install starlette-compress
```
## Basic Usage
### Starlette
```py
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_compress import CompressMiddleware
middleware = [
Middleware(CompressMiddleware)
]
app = Starlette(routes=..., middleware=middleware)
```
### FastAPI
You can use starlette-compress with [FastAPI](https://fastapi.tiangolo.com) too:
```py
from fastapi import FastAPI
from starlette_compress import CompressMiddleware
app = FastAPI()
app.add_middleware(CompressMiddleware)
```
## Advanced Usage
### Changing Minimum Response Size
Control the minimum size of the response to compress. By default, responses must be at least 500 bytes to be compressed.
```py
# Starlette
middleware = [
Middleware(CompressMiddleware, minimum_size=1000)
]
# FastAPI
app.add_middleware(CompressMiddleware, minimum_size=1000)
```
### Tuning Compression Levels
Adjust the compression levels for each algorithm. Higher levels mean smaller files but slower compression. Default level is 4 for all algorithms.
```py
# Starlette
middleware = [
Middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)
]
# FastAPI
app.add_middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)
```
### Supporting Custom Content-Types
Manage the supported content-types. Unknown response types are not compressed. [Check here](https://github.com/Zaczero/starlette-compress/blob/main/starlette_compress/__init__.py) for the default configuration.
```py
from starlette_compress import add_compress_type, remove_compress_type
add_compress_type("application/my-custom-type")
remove_compress_type("application/json")
```
Raw data
{
"_id": null,
"home_page": null,
"name": "starlette-compress",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "brotli, compression, fastapi, gzip, http, middleware, starlette, zstd",
"author": null,
"author_email": "Kamil Monicz <kamil@monicz.dev>",
"download_url": "https://files.pythonhosted.org/packages/13/17/c00c02e820e7535fe4c6eba8ee363073df513542ada14d3ee859f8605ed3/starlette_compress-1.5.0.tar.gz",
"platform": null,
"description": "# starlette-compress\n\n[](https://pypi.org/project/starlette-compress)\n[](https://liberapay.com/Zaczero/)\n[](https://github.com/sponsors/Zaczero)\n\n**starlette-compress** is a fast and simple middleware for compressing responses in [Starlette](https://www.starlette.io). It supports more compression algorithms than Starlette's built-in GZipMiddleware, and has more sensible defaults.\n\n- Python 3.9+ support\n- Compatible with `asyncio` and `trio` backends\n- ZStd, Brotli, and GZip compression\n- Sensible default configuration\n- [Zero-Clause BSD](https://choosealicense.com/licenses/0bsd/) \u2014 public domain dedication\n- [Semantic Versioning](https://semver.org) compliance\n\n## Installation\n\n```sh\npip install starlette-compress\n```\n\n## Basic Usage\n\n### Starlette\n\n```py\nfrom starlette.applications import Starlette\nfrom starlette.middleware import Middleware\nfrom starlette_compress import CompressMiddleware\n\nmiddleware = [\n Middleware(CompressMiddleware)\n]\n\napp = Starlette(routes=..., middleware=middleware)\n```\n\n### FastAPI\n\nYou can use starlette-compress with [FastAPI](https://fastapi.tiangolo.com) too:\n\n```py\nfrom fastapi import FastAPI\nfrom starlette_compress import CompressMiddleware\n\napp = FastAPI()\napp.add_middleware(CompressMiddleware)\n```\n\n## Advanced Usage\n\n### Changing Minimum Response Size\n\nControl the minimum size of the response to compress. By default, responses must be at least 500 bytes to be compressed.\n\n```py\n# Starlette\nmiddleware = [\n Middleware(CompressMiddleware, minimum_size=1000)\n]\n\n# FastAPI\napp.add_middleware(CompressMiddleware, minimum_size=1000)\n```\n\n### Tuning Compression Levels\n\nAdjust the compression levels for each algorithm. Higher levels mean smaller files but slower compression. Default level is 4 for all algorithms.\n\n```py\n# Starlette\nmiddleware = [\n Middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)\n]\n\n# FastAPI\napp.add_middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)\n```\n\n### Supporting Custom Content-Types\n\nManage the supported content-types. Unknown response types are not compressed. [Check here](https://github.com/Zaczero/starlette-compress/blob/main/starlette_compress/__init__.py) for the default configuration.\n\n```py\nfrom starlette_compress import add_compress_type, remove_compress_type\n\nadd_compress_type(\"application/my-custom-type\")\nremove_compress_type(\"application/json\")\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "Compression middleware for Starlette - supporting ZStd, Brotli, and GZip",
"version": "1.5.0",
"project_urls": null,
"split_keywords": [
"brotli",
" compression",
" fastapi",
" gzip",
" http",
" middleware",
" starlette",
" zstd"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "ed953e8405309f49956ac789ef6353a0244e243e73d13df12634b6dcfc0e1cbd",
"md5": "5e46f4420caed4cfa98c0f019facbea5",
"sha256": "1adae325d98c4e0d32928a1d9ba2f635c84b0dc743d8b90b90f30e122efe8219"
},
"downloads": -1,
"filename": "starlette_compress-1.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5e46f4420caed4cfa98c0f019facbea5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 6221,
"upload_time": "2025-02-08T03:37:42",
"upload_time_iso_8601": "2025-02-08T03:37:42.589300Z",
"url": "https://files.pythonhosted.org/packages/ed/95/3e8405309f49956ac789ef6353a0244e243e73d13df12634b6dcfc0e1cbd/starlette_compress-1.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "1317c00c02e820e7535fe4c6eba8ee363073df513542ada14d3ee859f8605ed3",
"md5": "46aa144967f6c1f6c7da54d097146646",
"sha256": "c62bce9295e35b465036af22129e8cc824957d0b3b50abe6c94e61e3fdcc320e"
},
"downloads": -1,
"filename": "starlette_compress-1.5.0.tar.gz",
"has_sig": false,
"md5_digest": "46aa144967f6c1f6c7da54d097146646",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 8314,
"upload_time": "2025-02-08T03:37:43",
"upload_time_iso_8601": "2025-02-08T03:37:43.958087Z",
"url": "https://files.pythonhosted.org/packages/13/17/c00c02e820e7535fe4c6eba8ee363073df513542ada14d3ee859f8605ed3/starlette_compress-1.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-08 03:37:43",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "starlette-compress"
}