Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_cors Discord

Swarmauri Middleware CORS

Custom CORS middleware for the Swarmauri framework that integrates with FastAPI applications.

Features

  • Handles CORS preflight (OPTIONS) requests directly and returns a 200 response with your configured headers.
  • Adds CORS headers to downstream responses produced by your FastAPI routes.
  • Rejects requests with a missing Origin header or an origin that is not explicitly allowed, returning a 403 response.
  • Allows fine-grained control of the exposed headers, allowed methods, credentials support, and cache duration of the CORS policy.

Installation

pip

pip install swarmauri_middleware_cors

Poetry

poetry add swarmauri_middleware_cors

uv

uv venv
source .venv/bin/activate  # Use .\.venv\Scripts\activate on Windows
uv add swarmauri_middleware_cors

Usage

Configure the middleware with the exact origins, methods, and headers you want to allow. Origins are matched literally?if you use the default ['*'] setting, every request must send an Origin: * header. In practice you should list the concrete origins your frontend sends.

Python example

from fastapi import FastAPI
from fastapi.testclient import TestClient

from swarmauri_middleware_cors import CustomCORSMiddleware

app = FastAPI()

cors = CustomCORSMiddleware(
    allow_origins=["https://frontend.example"],
    allow_methods=["GET", "OPTIONS"],
    allow_headers=["Authorization"],
    expose_headers=["X-App-Version"],
    max_age=300,
)


@app.middleware("http")
async def apply_custom_cors(request, call_next):
    return await cors.dispatch(request, call_next)


@app.get("/ping")
async def ping():
    return {"status": "ok"}


client = TestClient(app)

if __name__ == "__main__":
    options_response = client.options(
        "/ping", headers={"Origin": "https://frontend.example"}
    )
    print(
        "Allowed origin header:",
        options_response.headers["Access-Control-Allow-Origin"],
    )

    response = client.get(
        "/ping", headers={"Origin": "https://frontend.example"}
    )
    print("Response JSON:", response.json())

Running the example prints the allowed origin configured on the middleware and the JSON payload returned by the GET /ping route.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

Metadata

Release files for swarmauri_middleware_cors 0.11.0.dev1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swarmauri_middleware_cors 0.11.0.dev1
File Size Uploaded
swarmauri_middleware_cors-0.11.0.dev1.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swarmauri_middleware_cors 0.11.0.dev1
File Interpreter ABI Platform
swarmauri_middleware_cors-0.11.0.dev1-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / swarmauri_middleware_cors-0.11.0.dev1.tar.gz

Download URL swarmauri_middleware_cors-0.11.0.dev1.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
62eff183e21d8bdd76f34004e1a66c14cddcc2407920ba763d2924b06260afb8
BLAKE2b-256 checksum
How to use checksums
c64ba46d2e7b8da68f7d02e9beca09710ea441234bbb1bfe37a75edb1ca54619
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}

Release files / swarmauri_middleware_cors-0.11.0.dev1-py3-none-any.whl

Download URL swarmauri_middleware_cors-0.11.0.dev1-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb5fdb03499e3767402b006ce61b4b31848c08067a6fada13aa858d78e65925d
BLAKE2b-256 checksum
How to use checksums
caa8eec4b2c50e0f7bfa4bacf9fe9f113b36971ef81149fb5fa843b2815fa30d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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}
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